Skip to content

Commit 8c26f53

Browse files
committed
🔧
1 parent 96bcb6b commit 8c26f53

11 files changed

Lines changed: 1106 additions & 1166 deletions

File tree

apps/example/ios/Podfile.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ PODS:
17481748
- React-RCTFBReactNativeSpec
17491749
- ReactCommon/turbomodule/core
17501750
- SocketRocket
1751-
- react-native-safe-area-context (5.6.1):
1751+
- react-native-safe-area-context (5.6.2):
17521752
- boost
17531753
- DoubleConversion
17541754
- fast_float
@@ -1766,8 +1766,8 @@ PODS:
17661766
- React-graphics
17671767
- React-ImageManager
17681768
- React-jsi
1769-
- react-native-safe-area-context/common (= 5.6.1)
1770-
- react-native-safe-area-context/fabric (= 5.6.1)
1769+
- react-native-safe-area-context/common (= 5.6.2)
1770+
- react-native-safe-area-context/fabric (= 5.6.2)
17711771
- React-NativeModulesApple
17721772
- React-RCTFabric
17731773
- React-renderercss
@@ -1778,7 +1778,7 @@ PODS:
17781778
- ReactCommon/turbomodule/core
17791779
- SocketRocket
17801780
- Yoga
1781-
- react-native-safe-area-context/common (5.6.1):
1781+
- react-native-safe-area-context/common (5.6.2):
17821782
- boost
17831783
- DoubleConversion
17841784
- fast_float
@@ -1806,7 +1806,7 @@ PODS:
18061806
- ReactCommon/turbomodule/core
18071807
- SocketRocket
18081808
- Yoga
1809-
- react-native-safe-area-context/fabric (5.6.1):
1809+
- react-native-safe-area-context/fabric (5.6.2):
18101810
- boost
18111811
- DoubleConversion
18121812
- fast_float
@@ -2398,7 +2398,7 @@ PODS:
23982398
- React-perflogger (= 0.81.4)
23992399
- React-utils (= 0.81.4)
24002400
- SocketRocket
2401-
- ReactNativeHost (0.5.13):
2401+
- ReactNativeHost (0.5.15):
24022402
- boost
24032403
- DoubleConversion
24042404
- fast_float
@@ -2432,7 +2432,7 @@ PODS:
24322432
- React-Core
24332433
- React-jsi
24342434
- ReactTestApp-Resources (1.0.0-dev)
2435-
- RNGestureHandler (2.28.0):
2435+
- RNGestureHandler (2.30.0):
24362436
- boost
24372437
- DoubleConversion
24382438
- fast_float
@@ -2901,7 +2901,7 @@ SPEC CHECKSUMS:
29012901
React-logger: a3cb5b29c32b8e447b5a96919340e89334062b48
29022902
React-Mapbuffer: 9d2434a42701d6144ca18f0ca1c4507808ca7696
29032903
React-microtasksnativemodule: 75b6604b667d297292345302cc5bfb6b6aeccc1b
2904-
react-native-safe-area-context: c6e2edd1c1da07bdce287fa9d9e60c5f7b514616
2904+
react-native-safe-area-context: c00143b4823773bba23f2f19f85663ae89ceb460
29052905
react-native-skia: 5bf2b2107cd7f2d806fd364f5e16b1c7554ed3cd
29062906
react-native-wgpu: e54fcee5946cc2cee4814f63f425be358f097b14
29072907
React-NativeModulesApple: 879fbdc5dcff7136abceb7880fe8a2022a1bd7c3
@@ -2934,10 +2934,10 @@ SPEC CHECKSUMS:
29342934
ReactAppDependencyProvider: 433ddfb4536948630aadd5bd925aff8a632d2fe3
29352935
ReactCodegen: 64dbbed4e9e0264d799578ea78492479a66fba4a
29362936
ReactCommon: 394c6b92765cf6d211c2c3f7f6bc601dffb316a6
2937-
ReactNativeHost: 40e374201201cc54f9ef41458f2e412fbdde0d62
2937+
ReactNativeHost: f5e054387e917216a2a021a3f7fdc4f9f158e7e4
29382938
ReactTestApp-DevSupport: 9b7bbba5e8fed998e763809171d9906a1375f9d3
29392939
ReactTestApp-Resources: 1bd9ff10e4c24f2ad87101a32023721ae923bccf
2940-
RNGestureHandler: 3a73f098d74712952870e948b3d9cf7b6cae9961
2940+
RNGestureHandler: e37bdb684df1ac17c7e1d8f71a3311b2793c186b
29412941
RNReanimated: 4e53390354d1eed1398ab51ace22b869be6ce611
29422942
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
29432943
Yoga: a3ed390a19db0459bd6839823a6ac6d9c6db198d

packages/webgpu/cpp/jsi/NativeObject.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ class NativeObject : public jsi::NativeState,
144144
entry.prototype = std::move(prototype);
145145
}
146146

147+
/**
148+
* Install a constructor function on the global object.
149+
* This enables `instanceof` checks: `obj instanceof ClassName`
150+
*
151+
* The constructor throws if called directly (these objects are only
152+
* created internally by the native code).
153+
*/
154+
static void installConstructor(jsi::Runtime &runtime) {
155+
installPrototype(runtime);
156+
157+
auto &entry = getPrototypeCache().get(runtime);
158+
if (!entry.prototype.has_value()) {
159+
return;
160+
}
161+
162+
// Create a constructor function that throws when called directly
163+
auto ctor = jsi::Function::createFromHostFunction(
164+
runtime, jsi::PropNameID::forUtf8(runtime, Derived::CLASS_NAME), 0,
165+
[](jsi::Runtime &rt, const jsi::Value & /*thisVal*/,
166+
const jsi::Value * /*args*/, size_t /*count*/) -> jsi::Value {
167+
throw jsi::JSError(
168+
rt, std::string("Illegal constructor: ") + Derived::CLASS_NAME +
169+
" objects are created by the WebGPU API");
170+
});
171+
172+
// Set the prototype property on the constructor
173+
// This is what makes `instanceof` work
174+
ctor.setProperty(runtime, "prototype", *entry.prototype);
175+
176+
// Set constructor property on prototype pointing back to constructor
177+
entry.prototype->setProperty(runtime, "constructor", ctor);
178+
179+
// Install on global
180+
runtime.global().setProperty(runtime, Derived::CLASS_NAME, std::move(ctor));
181+
}
182+
147183
/**
148184
* Create a JS object with native state attached.
149185
*/

packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33
#include "GPU.h"
44
#include "RNWebGPU.h"
55

6+
// GPU API classes (for instanceof support)
7+
#include "GPUAdapter.h"
8+
#include "GPUAdapterInfo.h"
9+
#include "GPUBindGroup.h"
10+
#include "GPUBindGroupLayout.h"
11+
#include "GPUBuffer.h"
12+
#include "GPUCanvasContext.h"
13+
#include "GPUCommandBuffer.h"
14+
#include "GPUCommandEncoder.h"
15+
#include "GPUCompilationInfo.h"
16+
#include "GPUCompilationMessage.h"
17+
#include "GPUComputePassEncoder.h"
18+
#include "GPUComputePipeline.h"
19+
#include "GPUDevice.h"
20+
#include "GPUDeviceLostInfo.h"
21+
#include "GPUExternalTexture.h"
22+
#include "GPUPipelineLayout.h"
23+
#include "GPUQuerySet.h"
24+
#include "GPUQueue.h"
25+
#include "GPURenderBundle.h"
26+
#include "GPURenderBundleEncoder.h"
27+
#include "GPURenderPassEncoder.h"
28+
#include "GPURenderPipeline.h"
29+
#include "GPUSampler.h"
30+
#include "GPUShaderModule.h"
31+
#include "GPUSupportedLimits.h"
32+
#include "GPUTexture.h"
33+
#include "GPUTextureView.h"
34+
635
// Enums
736
#include "GPUBufferUsage.h"
837
#include "GPUColorWrite.h"
@@ -28,29 +57,47 @@ RNWebGPUManager::RNWebGPUManager(
2857
_jsRuntime->global().setProperty(*_jsRuntime, "RNWebGPU",
2958
RNWebGPU::create(*_jsRuntime, rnWebGPU));
3059

31-
auto bufferUsage = std::make_shared<GPUBufferUsage>();
32-
_jsRuntime->global().setProperty(
33-
*_jsRuntime, "GPUBufferUsage",
34-
GPUBufferUsage::create(*_jsRuntime, std::move(bufferUsage)));
35-
36-
auto colorWrite = std::make_shared<GPUColorWrite>();
37-
_jsRuntime->global().setProperty(
38-
*_jsRuntime, "GPUColorWrite",
39-
GPUColorWrite::create(*_jsRuntime, std::move(colorWrite)));
60+
// Install constructors for instanceof support
61+
GPU::installConstructor(*_jsRuntime);
62+
GPUAdapter::installConstructor(*_jsRuntime);
63+
GPUAdapterInfo::installConstructor(*_jsRuntime);
64+
GPUBindGroup::installConstructor(*_jsRuntime);
65+
GPUBindGroupLayout::installConstructor(*_jsRuntime);
66+
GPUBuffer::installConstructor(*_jsRuntime);
67+
GPUCanvasContext::installConstructor(*_jsRuntime);
68+
GPUCommandBuffer::installConstructor(*_jsRuntime);
69+
GPUCommandEncoder::installConstructor(*_jsRuntime);
70+
GPUCompilationInfo::installConstructor(*_jsRuntime);
71+
GPUCompilationMessage::installConstructor(*_jsRuntime);
72+
GPUComputePassEncoder::installConstructor(*_jsRuntime);
73+
GPUComputePipeline::installConstructor(*_jsRuntime);
74+
GPUDevice::installConstructor(*_jsRuntime);
75+
GPUDeviceLostInfo::installConstructor(*_jsRuntime);
76+
GPUExternalTexture::installConstructor(*_jsRuntime);
77+
GPUPipelineLayout::installConstructor(*_jsRuntime);
78+
GPUQuerySet::installConstructor(*_jsRuntime);
79+
GPUQueue::installConstructor(*_jsRuntime);
80+
GPURenderBundle::installConstructor(*_jsRuntime);
81+
GPURenderBundleEncoder::installConstructor(*_jsRuntime);
82+
GPURenderPassEncoder::installConstructor(*_jsRuntime);
83+
GPURenderPipeline::installConstructor(*_jsRuntime);
84+
GPUSampler::installConstructor(*_jsRuntime);
85+
GPUShaderModule::installConstructor(*_jsRuntime);
86+
GPUSupportedLimits::installConstructor(*_jsRuntime);
87+
GPUTexture::installConstructor(*_jsRuntime);
88+
GPUTextureView::installConstructor(*_jsRuntime);
4089

41-
auto mapMode = std::make_shared<GPUMapMode>();
90+
// Install constant objects as plain JS objects with own properties
91+
_jsRuntime->global().setProperty(*_jsRuntime, "GPUBufferUsage",
92+
GPUBufferUsage::create(*_jsRuntime));
93+
_jsRuntime->global().setProperty(*_jsRuntime, "GPUColorWrite",
94+
GPUColorWrite::create(*_jsRuntime));
4295
_jsRuntime->global().setProperty(*_jsRuntime, "GPUMapMode",
43-
GPUMapMode::create(*_jsRuntime, mapMode));
44-
45-
auto shaderStage = std::make_shared<GPUShaderStage>();
46-
_jsRuntime->global().setProperty(
47-
*_jsRuntime, "GPUShaderStage",
48-
GPUShaderStage::create(*_jsRuntime, std::move(shaderStage)));
49-
50-
auto textureUsage = std::make_shared<GPUTextureUsage>();
51-
_jsRuntime->global().setProperty(
52-
*_jsRuntime, "GPUTextureUsage",
53-
GPUTextureUsage::create(*_jsRuntime, std::move(textureUsage)));
96+
GPUMapMode::create(*_jsRuntime));
97+
_jsRuntime->global().setProperty(*_jsRuntime, "GPUShaderStage",
98+
GPUShaderStage::create(*_jsRuntime));
99+
_jsRuntime->global().setProperty(*_jsRuntime, "GPUTextureUsage",
100+
GPUTextureUsage::create(*_jsRuntime));
54101
}
55102

56103
RNWebGPUManager::~RNWebGPUManager() {
Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
11
#pragma once
2-
#include <string>
32

4-
#include <NativeObject.h>
3+
#include <jsi/jsi.h>
54

65
#include "webgpu/webgpu_cpp.h"
76

87
namespace rnwgpu {
98

109
namespace jsi = facebook::jsi;
1110

12-
class GPUBufferUsage : public NativeObject<GPUBufferUsage> {
11+
class GPUBufferUsage {
1312
public:
14-
static constexpr const char *CLASS_NAME = "GPUBufferUsage";
15-
16-
GPUBufferUsage() : NativeObject(CLASS_NAME) {}
17-
18-
public:
19-
double MapRead() { return static_cast<double>(wgpu::BufferUsage::MapRead); }
20-
double MapWrite() { return static_cast<double>(wgpu::BufferUsage::MapWrite); }
21-
double CopySrc() { return static_cast<double>(wgpu::BufferUsage::CopySrc); }
22-
double CopyDst() { return static_cast<double>(wgpu::BufferUsage::CopyDst); }
23-
double Index() { return static_cast<double>(wgpu::BufferUsage::Index); }
24-
double Vertex() { return static_cast<double>(wgpu::BufferUsage::Vertex); }
25-
double Uniform() { return static_cast<double>(wgpu::BufferUsage::Uniform); }
26-
double Storage() { return static_cast<double>(wgpu::BufferUsage::Storage); }
27-
double Indirect() { return static_cast<double>(wgpu::BufferUsage::Indirect); }
28-
double QueryResolve() {
29-
return static_cast<double>(wgpu::BufferUsage::QueryResolve);
30-
}
31-
32-
static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
33-
installGetter(runtime, prototype, "MAP_READ", &GPUBufferUsage::MapRead);
34-
installGetter(runtime, prototype, "MAP_WRITE", &GPUBufferUsage::MapWrite);
35-
installGetter(runtime, prototype, "COPY_SRC", &GPUBufferUsage::CopySrc);
36-
installGetter(runtime, prototype, "COPY_DST", &GPUBufferUsage::CopyDst);
37-
installGetter(runtime, prototype, "INDEX", &GPUBufferUsage::Index);
38-
installGetter(runtime, prototype, "VERTEX", &GPUBufferUsage::Vertex);
39-
installGetter(runtime, prototype, "UNIFORM", &GPUBufferUsage::Uniform);
40-
installGetter(runtime, prototype, "STORAGE", &GPUBufferUsage::Storage);
41-
installGetter(runtime, prototype, "INDIRECT", &GPUBufferUsage::Indirect);
42-
installGetter(runtime, prototype, "QUERY_RESOLVE",
43-
&GPUBufferUsage::QueryResolve);
13+
static jsi::Object create(jsi::Runtime &runtime) {
14+
jsi::Object obj(runtime);
15+
obj.setProperty(runtime, "MAP_READ",
16+
static_cast<double>(wgpu::BufferUsage::MapRead));
17+
obj.setProperty(runtime, "MAP_WRITE",
18+
static_cast<double>(wgpu::BufferUsage::MapWrite));
19+
obj.setProperty(runtime, "COPY_SRC",
20+
static_cast<double>(wgpu::BufferUsage::CopySrc));
21+
obj.setProperty(runtime, "COPY_DST",
22+
static_cast<double>(wgpu::BufferUsage::CopyDst));
23+
obj.setProperty(runtime, "INDEX",
24+
static_cast<double>(wgpu::BufferUsage::Index));
25+
obj.setProperty(runtime, "VERTEX",
26+
static_cast<double>(wgpu::BufferUsage::Vertex));
27+
obj.setProperty(runtime, "UNIFORM",
28+
static_cast<double>(wgpu::BufferUsage::Uniform));
29+
obj.setProperty(runtime, "STORAGE",
30+
static_cast<double>(wgpu::BufferUsage::Storage));
31+
obj.setProperty(runtime, "INDIRECT",
32+
static_cast<double>(wgpu::BufferUsage::Indirect));
33+
obj.setProperty(runtime, "QUERY_RESOLVE",
34+
static_cast<double>(wgpu::BufferUsage::QueryResolve));
35+
return obj;
4436
}
4537
};
38+
4639
} // namespace rnwgpu
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
#pragma once
2-
#include <string>
32

4-
#include <NativeObject.h>
3+
#include <jsi/jsi.h>
54

65
#include "webgpu/webgpu_cpp.h"
76

87
namespace rnwgpu {
98

109
namespace jsi = facebook::jsi;
1110

12-
class GPUColorWrite : public NativeObject<GPUColorWrite> {
11+
class GPUColorWrite {
1312
public:
14-
static constexpr const char *CLASS_NAME = "GPUColorWrite";
15-
16-
GPUColorWrite() : NativeObject(CLASS_NAME) {}
17-
18-
public:
19-
double Red() { return static_cast<double>(wgpu::ColorWriteMask::Red); }
20-
double Green() { return static_cast<double>(wgpu::ColorWriteMask::Green); }
21-
double Blue() { return static_cast<double>(wgpu::ColorWriteMask::Blue); }
22-
double Alpha() { return static_cast<double>(wgpu::ColorWriteMask::Alpha); }
23-
double All() { return static_cast<double>(wgpu::ColorWriteMask::All); }
24-
25-
static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
26-
installGetter(runtime, prototype, "RED", &GPUColorWrite::Red);
27-
installGetter(runtime, prototype, "GREEN", &GPUColorWrite::Green);
28-
installGetter(runtime, prototype, "BLUE", &GPUColorWrite::Blue);
29-
installGetter(runtime, prototype, "ALPHA", &GPUColorWrite::Alpha);
30-
installGetter(runtime, prototype, "ALL", &GPUColorWrite::All);
13+
static jsi::Object create(jsi::Runtime &runtime) {
14+
jsi::Object obj(runtime);
15+
obj.setProperty(runtime, "RED",
16+
static_cast<double>(wgpu::ColorWriteMask::Red));
17+
obj.setProperty(runtime, "GREEN",
18+
static_cast<double>(wgpu::ColorWriteMask::Green));
19+
obj.setProperty(runtime, "BLUE",
20+
static_cast<double>(wgpu::ColorWriteMask::Blue));
21+
obj.setProperty(runtime, "ALPHA",
22+
static_cast<double>(wgpu::ColorWriteMask::Alpha));
23+
obj.setProperty(runtime, "ALL",
24+
static_cast<double>(wgpu::ColorWriteMask::All));
25+
return obj;
3126
}
3227
};
28+
3329
} // namespace rnwgpu
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
#pragma once
2-
#include <string>
32

4-
#include <NativeObject.h>
3+
#include <jsi/jsi.h>
54

65
#include "webgpu/webgpu_cpp.h"
76

87
namespace rnwgpu {
98

109
namespace jsi = facebook::jsi;
1110

12-
class GPUMapMode : public NativeObject<GPUMapMode> {
11+
class GPUMapMode {
1312
public:
14-
static constexpr const char *CLASS_NAME = "GPUMapMode";
15-
16-
GPUMapMode() : NativeObject(CLASS_NAME) {}
17-
18-
public:
19-
double Read() { return static_cast<double>(wgpu::MapMode::Read); }
20-
double Write() { return static_cast<double>(wgpu::MapMode::Write); }
21-
22-
static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
23-
installGetter(runtime, prototype, "READ", &GPUMapMode::Read);
24-
installGetter(runtime, prototype, "WRITE", &GPUMapMode::Write);
13+
static jsi::Object create(jsi::Runtime &runtime) {
14+
jsi::Object obj(runtime);
15+
obj.setProperty(runtime, "READ",
16+
static_cast<double>(wgpu::MapMode::Read));
17+
obj.setProperty(runtime, "WRITE",
18+
static_cast<double>(wgpu::MapMode::Write));
19+
return obj;
2520
}
2621
};
22+
2723
} // namespace rnwgpu

0 commit comments

Comments
 (0)