Repository navigation
Commit 53f9373
committed
fix: route Java->JS calls to the runtime that created the binding instance
Binding 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.
Both generators now emit com.tns.NativeScriptRuntimeBound: the static binding
generator in Java source, and the runtime binding generator - which dexes the
proxies behind new java.lang.Runnable({...}) and is the more common of the two -
in asmdex alongside the __initialized field and equals__super it already emits.
initInstance stamps the id once; later registrations by other runtimes
(getOrCreateJavaObjectID, when the instance crosses into another isolate) must
not overwrite it, because only the creating runtime holds the JS implementation
behind the generated overrides.
INVALID_RUNTIME_ID is -1, so both generators seed the field rather than rely on
its zero default: 0 is the main runtime's id, and JsV8InspectorClient hard-codes
it as such. The dex proxies seed it as early as a constructor may touch the
instance - the verifier rejects field access on an uninitialized reference, so
not before the superclass constructor. A proxied method invoked from within that
superclass constructor therefore still reads 0 and resolves to the main runtime,
which finds no object id registered for the instance and reports that; before
this change the same call reported that no runtime held the instance.
A call with nowhere left to run is 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.
Bindings from an older generator carry no id and keep the previous thread-based
resolution. Costs two methods and one int field per binding class; noted for dex
budgets.1 parent 8a4ac28 commit 53f9373
14 files changed
Lines changed: 300 additions & 23 deletions
File tree
- test-app
- app/src/main/assets/app/tests
- 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
- bindings
- runtime/src/main/java/com/tns
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| |||
| 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 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
| 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 | | |
| |||
| 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 | | |
| |||
| 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 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
0 commit comments