react-native-webgpu 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -15
- package/cpp/jsi/NativeObject.h +77 -8
- package/cpp/jsi/RuntimeAwareCache.cpp +1 -1
- package/cpp/jsi/RuntimeAwareCache.h +25 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,20 +8,6 @@ WebGPU for React Native, powered by Dawn.
|
|
|
8
8
|
|
|
9
9
|
[Installation instructions](https://wcandillon.github.io/react-native-webgpu/docs/getting-started/installation)
|
|
10
10
|
|
|
11
|
-
## Expo config plugin
|
|
12
|
-
|
|
13
|
-
The package ships with an Expo config plugin that disables Metal API validation in the iOS Xcode scheme, which is required to run WebGPU on the iOS Simulator (Metal API validation reports false positives for Dawn that do not occur on device). Because the plugin is part of the package, plugin and package versions are always aligned.
|
|
14
|
-
|
|
15
|
-
```json
|
|
16
|
-
{
|
|
17
|
-
"expo": {
|
|
18
|
-
"plugins": ["react-native-webgpu"]
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
See the [Expo guide](https://wcandillon.github.io/react-native-webgpu/docs/getting-started/expo#config-plugin) for details.
|
|
24
|
-
|
|
25
11
|
## Contributing
|
|
26
12
|
|
|
27
|
-
For detailed information on library development, building, testing, and contributing guidelines, please see [CONTRIBUTING.md](packages/webgpu/CONTRIBUTING.md).
|
|
13
|
+
For detailed information on library development, building, testing, and contributing guidelines, please see [CONTRIBUTING.md](packages/webgpu/CONTRIBUTING.md).
|
package/cpp/jsi/NativeObject.h
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
#include <memory>
|
|
10
10
|
#include <mutex>
|
|
11
11
|
#include <optional>
|
|
12
|
+
#include <stdexcept>
|
|
12
13
|
#include <string>
|
|
13
14
|
#include <type_traits>
|
|
14
15
|
#include <unordered_map>
|
|
@@ -48,6 +49,12 @@ public:
|
|
|
48
49
|
|
|
49
50
|
void registerInstaller(const std::string &brand, InstallerFunc installer) {
|
|
50
51
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
52
|
+
if (_installers.count(brand) != 0) {
|
|
53
|
+
// The brand is the key unbox() uses to rebuild objects on worklet
|
|
54
|
+
// runtimes - a duplicate would let one class hijack another's unboxing.
|
|
55
|
+
throw std::runtime_error("Duplicate native object brand registered: " +
|
|
56
|
+
brand);
|
|
57
|
+
}
|
|
51
58
|
_installers[brand] = std::move(installer);
|
|
52
59
|
}
|
|
53
60
|
|
|
@@ -235,6 +242,10 @@ public:
|
|
|
235
242
|
* Each NativeObject<Derived> type has its own static cache.
|
|
236
243
|
* Uses StaticRuntimeAwareCache to properly handle runtime lifecycle
|
|
237
244
|
* and hot reload (where the main runtime is destroyed and recreated).
|
|
245
|
+
*
|
|
246
|
+
* Callers must hold getPrototypeCacheMutex(): the cache is reached
|
|
247
|
+
* concurrently from the main JS thread (create()) and from worklet
|
|
248
|
+
* runtime threads (BoxedWebGPUObject::unbox() -> installPrototype()).
|
|
238
249
|
*/
|
|
239
250
|
static RuntimeAwareCache<PrototypeCacheEntry> &
|
|
240
251
|
getPrototypeCache(jsi::Runtime &runtime) {
|
|
@@ -242,11 +253,22 @@ public:
|
|
|
242
253
|
return cache.get(runtime);
|
|
243
254
|
}
|
|
244
255
|
|
|
256
|
+
/**
|
|
257
|
+
* Per-class mutex guarding getPrototypeCache() and prototype
|
|
258
|
+
* installation. Serializes the StaticRuntimeAwareCache pointer swap
|
|
259
|
+
* (hot reload) and the per-runtime cache lookups.
|
|
260
|
+
*/
|
|
261
|
+
static std::mutex &getPrototypeCacheMutex() {
|
|
262
|
+
static std::mutex mutex;
|
|
263
|
+
return mutex;
|
|
264
|
+
}
|
|
265
|
+
|
|
245
266
|
/**
|
|
246
267
|
* Ensure the prototype is installed for this runtime.
|
|
247
268
|
* Called automatically by create(), but can be called manually.
|
|
248
269
|
*/
|
|
249
270
|
static void installPrototype(jsi::Runtime &runtime) {
|
|
271
|
+
std::lock_guard<std::mutex> lock(getPrototypeCacheMutex());
|
|
250
272
|
auto &entry = getPrototypeCache(runtime).get(runtime);
|
|
251
273
|
if (entry.prototype.has_value()) {
|
|
252
274
|
return; // Already installed
|
|
@@ -278,6 +300,49 @@ public:
|
|
|
278
300
|
defineProperty.call(runtime, prototype, toStringTag, descriptor);
|
|
279
301
|
}
|
|
280
302
|
|
|
303
|
+
// Install a generic toJSON so JSON.stringify works: data properties live
|
|
304
|
+
// as getters on this shared prototype, and JSON.stringify only serializes
|
|
305
|
+
// own enumerable properties (so it would otherwise produce {}).
|
|
306
|
+
auto toJSON = jsi::Function::createFromHostFunction(
|
|
307
|
+
runtime, jsi::PropNameID::forUtf8(runtime, "toJSON"), 0,
|
|
308
|
+
[](jsi::Runtime &rt, const jsi::Value &thisVal,
|
|
309
|
+
const jsi::Value * /*args*/, size_t /*count*/) -> jsi::Value {
|
|
310
|
+
auto thisObj = thisVal.getObject(rt);
|
|
311
|
+
auto objectCtor = rt.global().getPropertyAsObject(rt, "Object");
|
|
312
|
+
auto getPrototypeOf =
|
|
313
|
+
objectCtor.getPropertyAsFunction(rt, "getPrototypeOf");
|
|
314
|
+
auto proto = getPrototypeOf.call(rt, thisObj);
|
|
315
|
+
jsi::Object result(rt);
|
|
316
|
+
if (!proto.isObject()) {
|
|
317
|
+
return std::move(result);
|
|
318
|
+
}
|
|
319
|
+
auto getOwnPropertyNames =
|
|
320
|
+
objectCtor.getPropertyAsFunction(rt, "getOwnPropertyNames");
|
|
321
|
+
auto names =
|
|
322
|
+
getOwnPropertyNames.call(rt, proto).getObject(rt).getArray(rt);
|
|
323
|
+
size_t length = names.size(rt);
|
|
324
|
+
for (size_t i = 0; i < length; i++) {
|
|
325
|
+
auto nameValue = names.getValueAtIndex(rt, i);
|
|
326
|
+
if (!nameValue.isString()) {
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
auto name = nameValue.getString(rt).utf8(rt);
|
|
330
|
+
if (name == "constructor" || name == "toJSON") {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
// Read off `this` so prototype getters evaluate against the
|
|
334
|
+
// object's native state. Getters that throw keep throwing.
|
|
335
|
+
auto value = thisObj.getProperty(rt, name.c_str());
|
|
336
|
+
if (value.isObject() && value.getObject(rt).isFunction(rt)) {
|
|
337
|
+
// Skip methods - JSON.stringify would drop them anyway
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
result.setProperty(rt, name.c_str(), value);
|
|
341
|
+
}
|
|
342
|
+
return std::move(result);
|
|
343
|
+
});
|
|
344
|
+
prototype.setProperty(runtime, "toJSON", toJSON);
|
|
345
|
+
|
|
281
346
|
// Cache the prototype
|
|
282
347
|
entry.prototype = std::move(prototype);
|
|
283
348
|
}
|
|
@@ -303,6 +368,7 @@ public:
|
|
|
303
368
|
|
|
304
369
|
installPrototype(runtime);
|
|
305
370
|
|
|
371
|
+
std::lock_guard<std::mutex> lock(getPrototypeCacheMutex());
|
|
306
372
|
auto &entry = getPrototypeCache(runtime).get(runtime);
|
|
307
373
|
if (!entry.prototype.has_value()) {
|
|
308
374
|
return;
|
|
@@ -346,14 +412,17 @@ public:
|
|
|
346
412
|
obj.setNativeState(runtime, instance);
|
|
347
413
|
|
|
348
414
|
// Set prototype
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
415
|
+
{
|
|
416
|
+
std::lock_guard<std::mutex> lock(getPrototypeCacheMutex());
|
|
417
|
+
auto &entry = getPrototypeCache(runtime).get(runtime);
|
|
418
|
+
if (entry.prototype.has_value()) {
|
|
419
|
+
// Use Object.setPrototypeOf to set the prototype
|
|
420
|
+
auto objectCtor =
|
|
421
|
+
runtime.global().getPropertyAsObject(runtime, "Object");
|
|
422
|
+
auto setPrototypeOf =
|
|
423
|
+
objectCtor.getPropertyAsFunction(runtime, "setPrototypeOf");
|
|
424
|
+
setPrototypeOf.call(runtime, obj, *entry.prototype);
|
|
425
|
+
}
|
|
357
426
|
}
|
|
358
427
|
|
|
359
428
|
// Set memory pressure hint for GC
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
#include <jsi/jsi.h>
|
|
4
4
|
|
|
5
|
+
#include <atomic>
|
|
5
6
|
#include <cassert>
|
|
6
7
|
#include <memory>
|
|
8
|
+
#include <mutex>
|
|
7
9
|
#include <unordered_map>
|
|
8
10
|
#include <utility>
|
|
9
11
|
|
|
@@ -15,17 +17,22 @@ namespace jsi = facebook::jsi;
|
|
|
15
17
|
|
|
16
18
|
class BaseRuntimeAwareCache {
|
|
17
19
|
public:
|
|
18
|
-
static void setMainJsRuntime(jsi::Runtime *rt) {
|
|
20
|
+
static void setMainJsRuntime(jsi::Runtime *rt) {
|
|
21
|
+
// Atomic: hot reload rewrites this from the JS thread while worklet
|
|
22
|
+
// runtime threads read it concurrently.
|
|
23
|
+
_mainRuntime.store(rt, std::memory_order_release);
|
|
24
|
+
}
|
|
19
25
|
static jsi::Runtime *getMainJsRuntime() {
|
|
20
|
-
|
|
26
|
+
auto *rt = _mainRuntime.load(std::memory_order_acquire);
|
|
27
|
+
assert(rt != nullptr &&
|
|
21
28
|
"Expected main Javascript runtime to be set in the "
|
|
22
29
|
"BaseRuntimeAwareCache class.");
|
|
23
30
|
|
|
24
|
-
return
|
|
31
|
+
return rt;
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
private:
|
|
28
|
-
static jsi::Runtime
|
|
35
|
+
static std::atomic<jsi::Runtime *> _mainRuntime;
|
|
29
36
|
};
|
|
30
37
|
|
|
31
38
|
/**
|
|
@@ -56,7 +63,10 @@ class RuntimeAwareCache : public BaseRuntimeAwareCache,
|
|
|
56
63
|
public:
|
|
57
64
|
void onRuntimeDestroyed(jsi::Runtime *rt) override {
|
|
58
65
|
if (getMainJsRuntime() != rt) {
|
|
59
|
-
// We are removing a secondary runtime
|
|
66
|
+
// We are removing a secondary runtime. This is invoked by
|
|
67
|
+
// RuntimeLifecycleMonitor on the destroyed runtime's thread, which may
|
|
68
|
+
// run concurrently with get() on another runtime's thread.
|
|
69
|
+
std::lock_guard<std::mutex> lock(_secondaryCachesMutex);
|
|
60
70
|
_secondaryRuntimeCaches.erase(rt);
|
|
61
71
|
}
|
|
62
72
|
}
|
|
@@ -75,6 +85,14 @@ public:
|
|
|
75
85
|
if (getMainJsRuntime() == &rt) {
|
|
76
86
|
return _primaryCache;
|
|
77
87
|
} else {
|
|
88
|
+
// Guard the secondary map: it can be mutated concurrently by get()
|
|
89
|
+
// on a worklet runtime's thread and by onRuntimeDestroyed() when
|
|
90
|
+
// another secondary runtime is torn down. The main-runtime path above
|
|
91
|
+
// stays lock-free. References into the map remain valid after the
|
|
92
|
+
// lock is released (unordered_map never invalidates references on
|
|
93
|
+
// insert/erase of other keys, and this runtime's entry can only be
|
|
94
|
+
// erased from this runtime's own thread).
|
|
95
|
+
std::lock_guard<std::mutex> lock(_secondaryCachesMutex);
|
|
78
96
|
if (_secondaryRuntimeCaches.count(&rt) == 0) {
|
|
79
97
|
// we only add listener when the secondary runtime is used, this assumes
|
|
80
98
|
// that the secondary runtime is terminated first. This lets us avoid
|
|
@@ -88,11 +106,12 @@ public:
|
|
|
88
106
|
T cache;
|
|
89
107
|
_secondaryRuntimeCaches.emplace(&rt, std::move(cache));
|
|
90
108
|
}
|
|
109
|
+
return _secondaryRuntimeCaches.at(&rt);
|
|
91
110
|
}
|
|
92
|
-
return _secondaryRuntimeCaches.at(&rt);
|
|
93
111
|
}
|
|
94
112
|
|
|
95
113
|
private:
|
|
114
|
+
std::mutex _secondaryCachesMutex;
|
|
96
115
|
std::unordered_map<void *, T> _secondaryRuntimeCaches;
|
|
97
116
|
T _primaryCache;
|
|
98
117
|
};
|