Commit 3d5fb2d
committed
fix: route Java->JS calls to the runtime that created the binding instance
SBG-generated classes carried no runtime identity, so com.tns.Runtime had to
re-derive the target of every Java->JS call from the calling thread: the
currentRuntime thread-local, then the entered V8 isolate, then a linear scan of
every live runtime's object maps (getObjectRuntime). That is wrong in two ways
and racy in a third.
Ownership was inferred from "some runtime has a wrapper for this object", but a
wrapper only records that the instance crossed into that isolate. An instance
created by one runtime and later passed into another exists in both maps, so a
call could dispatch into an isolate that holds a wrapper but not the user's JS
implementation. The scan also reads other runtimes' strongJavaObjectToID /
weakJavaObjectToID from a foreign thread; with enableMultithreadedJavascript
off (the default) those are plain HashMap/NativeScriptHashMap being mutated
concurrently by their owning threads.
Generated classes now implement com.tns.NativeScriptRuntimeBound and carry the
id of the runtime that registered them. initInstance stamps it once - later
registrations by other runtimes must not overwrite it, since only the creating
runtime holds the JS implementation behind the generated overrides. Runtime ids
come from a monotonic counter and are never reused, so a stale id can never
match a newer runtime. Instances generated before this interface existed are
tracked in a weak, identity-keyed side map instead, and fall back to the old
resolution when they predate even that.
A call with nowhere left to run is now dropped and logged rather than executed
against an unrelated runtime. These arrive on Android callbacks - lifecycle,
listeners, draw - so throwing would take down the process because a worker
ended; the loss stays contained to the one call. Primitive returns reuse the
existing default-value substitution so the generated cast can still unbox.
- callJSMethod against a terminated owner drops instead of scanning and
landing on whichever runtime happens to hold a wrapper.
- dispatchCallJSMethodNative ignored the result of threadScheduler.post(); a
post to a quitting worker looper already returned null silently, and now
says so in the log.
initInstance is the one case that still throws: it dereferenced a null runtime,
so constructing a binding off a runtime thread produced a bare NPE. Dropping
there would leave an unregistered instance whose every later call fails anyway,
further from the cause, so it reports instead.
passSuppressedExceptionToJs routed by calling thread, so an exception suppressed
on behalf of another runtime was reported to the wrong one - and the message it
built was never passed to passDiscardedExceptionToJs. It now takes the instance,
resolves the same owner, and reports on that runtime's own thread without
blocking. The two-argument overload stays for already-generated bindings.
Costs two methods and one int field per generated class; noted for dex budgets.1 parent 8a4ac28 commit 3d5fb2d
12 files changed
Lines changed: 256 additions & 16 deletions
File tree
- test-app
- build-tools/static-binding-generator/src
- main/java/org/nativescript/staticbindinggenerator
- generating/writing
- impl
- test/java
- android/util
- com/tns
- org/nativescript/staticbindinggenerator/test
- runtime-binding-generator/src/main/java/com/tns
- runtime/src/main/java/com/tns
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
448 | 448 | | |
449 | 449 | | |
450 | 450 | | |
| 451 | + | |
| 452 | + | |
451 | 453 | | |
452 | 454 | | |
453 | 455 | | |
| |||
523 | 525 | | |
524 | 526 | | |
525 | 527 | | |
| 528 | + | |
526 | 529 | | |
527 | 530 | | |
528 | 531 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
45 | 49 | | |
46 | 50 | | |
47 | 51 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
17 | 25 | | |
18 | 26 | | |
19 | 27 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
50 | 54 | | |
51 | 55 | | |
52 | 56 | | |
| |||
369 | 373 | | |
370 | 374 | | |
371 | 375 | | |
372 | | - | |
| 376 | + | |
373 | 377 | | |
374 | 378 | | |
375 | 379 | | |
| |||
398 | 402 | | |
399 | 403 | | |
400 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
401 | 411 | | |
402 | 412 | | |
403 | 413 | | |
| |||
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
Lines changed: 47 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
57 | 100 | | |
58 | 101 | | |
59 | 102 | | |
| |||
79 | 122 | | |
80 | 123 | | |
81 | 124 | | |
82 | | - | |
| 125 | + | |
83 | 126 | | |
84 | 127 | | |
85 | 128 | | |
| |||
100 | 143 | | |
101 | 144 | | |
102 | 145 | | |
103 | | - | |
| 146 | + | |
104 | 147 | | |
105 | 148 | | |
106 | 149 | | |
| |||
127 | 170 | | |
128 | 171 | | |
129 | 172 | | |
130 | | - | |
| 173 | + | |
131 | 174 | | |
132 | 175 | | |
133 | 176 | | |
| |||
0 commit comments