1 |
|
|
#include "node_wasm_web_api.h" |
2 |
|
|
|
3 |
|
|
#include "memory_tracker-inl.h" |
4 |
|
|
#include "node_errors.h" |
5 |
|
|
#include "node_external_reference.h" |
6 |
|
|
|
7 |
|
|
namespace node { |
8 |
|
|
namespace wasm_web_api { |
9 |
|
|
|
10 |
|
|
using v8::ArrayBuffer; |
11 |
|
|
using v8::ArrayBufferView; |
12 |
|
|
using v8::Context; |
13 |
|
|
using v8::Function; |
14 |
|
|
using v8::FunctionCallbackInfo; |
15 |
|
|
using v8::FunctionTemplate; |
16 |
|
|
using v8::Isolate; |
17 |
|
|
using v8::Local; |
18 |
|
|
using v8::MaybeLocal; |
19 |
|
|
using v8::Object; |
20 |
|
|
using v8::Value; |
21 |
|
|
using v8::WasmStreaming; |
22 |
|
|
|
23 |
|
228 |
Local<Function> WasmStreamingObject::Initialize(Environment* env) { |
24 |
|
228 |
Local<Function> templ = env->wasm_streaming_object_constructor(); |
25 |
✓✓ |
228 |
if (!templ.IsEmpty()) { |
26 |
|
220 |
return templ; |
27 |
|
|
} |
28 |
|
|
|
29 |
|
8 |
Isolate* isolate = env->isolate(); |
30 |
|
8 |
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New); |
31 |
|
8 |
t->Inherit(BaseObject::GetConstructorTemplate(env)); |
32 |
|
16 |
t->InstanceTemplate()->SetInternalFieldCount( |
33 |
|
|
WasmStreamingObject::kInternalFieldCount); |
34 |
|
|
|
35 |
|
8 |
SetProtoMethod(isolate, t, "setURL", SetURL); |
36 |
|
8 |
SetProtoMethod(isolate, t, "push", Push); |
37 |
|
8 |
SetProtoMethod(isolate, t, "finish", Finish); |
38 |
|
8 |
SetProtoMethod(isolate, t, "abort", Abort); |
39 |
|
|
|
40 |
|
8 |
auto function = t->GetFunction(env->context()).ToLocalChecked(); |
41 |
|
8 |
env->set_wasm_streaming_object_constructor(function); |
42 |
|
8 |
return function; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
5320 |
void WasmStreamingObject::RegisterExternalReferences( |
46 |
|
|
ExternalReferenceRegistry* registry) { |
47 |
|
5320 |
registry->Register(New); |
48 |
|
5320 |
registry->Register(Push); |
49 |
|
5320 |
registry->Register(Finish); |
50 |
|
5320 |
registry->Register(Abort); |
51 |
|
5320 |
} |
52 |
|
|
|
53 |
|
|
void WasmStreamingObject::MemoryInfo(MemoryTracker* tracker) const { |
54 |
|
|
// v8::WasmStreaming is opaque. We assume that the size of the WebAssembly |
55 |
|
|
// module that is being compiled is roughly what V8 allocates (as in, off by |
56 |
|
|
// only a small factor). |
57 |
|
|
tracker->TrackFieldWithSize("streaming", wasm_size_); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
228 |
MaybeLocal<Object> WasmStreamingObject::Create( |
61 |
|
|
Environment* env, std::shared_ptr<WasmStreaming> streaming) { |
62 |
|
228 |
Local<Function> ctor = Initialize(env); |
63 |
|
|
Local<Object> obj; |
64 |
✗✓ |
456 |
if (!ctor->NewInstance(env->context(), 0, nullptr).ToLocal(&obj)) { |
65 |
|
|
return MaybeLocal<Object>(); |
66 |
|
|
} |
67 |
|
|
|
68 |
✗✓ |
228 |
CHECK(streaming); |
69 |
|
|
|
70 |
|
228 |
WasmStreamingObject* ptr = Unwrap<WasmStreamingObject>(obj); |
71 |
✗✓ |
228 |
CHECK_NOT_NULL(ptr); |
72 |
|
228 |
ptr->streaming_ = streaming; |
73 |
|
228 |
ptr->wasm_size_ = 0; |
74 |
|
228 |
return obj; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
228 |
void WasmStreamingObject::New(const FunctionCallbackInfo<Value>& args) { |
78 |
✗✓ |
228 |
CHECK(args.IsConstructCall()); |
79 |
|
228 |
Environment* env = Environment::GetCurrent(args); |
80 |
|
228 |
new WasmStreamingObject(env, args.This()); |
81 |
|
228 |
} |
82 |
|
|
|
83 |
|
12 |
void WasmStreamingObject::SetURL(const FunctionCallbackInfo<Value>& args) { |
84 |
|
|
WasmStreamingObject* obj; |
85 |
✗✓ |
12 |
ASSIGN_OR_RETURN_UNWRAP(&obj, args.Holder()); |
86 |
✗✓ |
12 |
CHECK(obj->streaming_); |
87 |
|
|
|
88 |
✗✓ |
12 |
CHECK_EQ(args.Length(), 1); |
89 |
✗✓ |
24 |
CHECK(args[0]->IsString()); |
90 |
|
24 |
Utf8Value url(Environment::GetCurrent(args)->isolate(), args[0]); |
91 |
|
12 |
obj->streaming_->SetUrl(url.out(), url.length()); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
352 |
void WasmStreamingObject::Push(const FunctionCallbackInfo<Value>& args) { |
95 |
|
|
WasmStreamingObject* obj; |
96 |
✗✓ |
352 |
ASSIGN_OR_RETURN_UNWRAP(&obj, args.Holder()); |
97 |
✗✓ |
352 |
CHECK(obj->streaming_); |
98 |
|
|
|
99 |
✗✓ |
352 |
CHECK_EQ(args.Length(), 1); |
100 |
|
352 |
Local<Value> chunk = args[0]; |
101 |
|
|
|
102 |
|
|
// The start of the memory section backing the ArrayBuffer(View), the offset |
103 |
|
|
// of the ArrayBuffer(View) within the memory section, and its size in bytes. |
104 |
|
|
const void* bytes; |
105 |
|
|
size_t offset; |
106 |
|
|
size_t size; |
107 |
|
|
|
108 |
✓✓ |
352 |
if (LIKELY(chunk->IsArrayBufferView())) { |
109 |
|
350 |
Local<ArrayBufferView> view = chunk.As<ArrayBufferView>(); |
110 |
|
700 |
bytes = view->Buffer()->Data(); |
111 |
|
350 |
offset = view->ByteOffset(); |
112 |
|
350 |
size = view->ByteLength(); |
113 |
✓✗ |
2 |
} else if (LIKELY(chunk->IsArrayBuffer())) { |
114 |
|
2 |
Local<ArrayBuffer> buffer = chunk.As<ArrayBuffer>(); |
115 |
|
2 |
bytes = buffer->Data(); |
116 |
|
2 |
offset = 0; |
117 |
|
2 |
size = buffer->ByteLength(); |
118 |
|
|
} else { |
119 |
|
|
return node::THROW_ERR_INVALID_ARG_TYPE( |
120 |
|
|
Environment::GetCurrent(args), |
121 |
|
|
"chunk must be an ArrayBufferView or an ArrayBuffer"); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
// Forward the data to V8. Internally, V8 will make a copy. |
125 |
|
352 |
obj->streaming_->OnBytesReceived(static_cast<const uint8_t*>(bytes) + offset, |
126 |
|
|
size); |
127 |
|
352 |
obj->wasm_size_ += size; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
154 |
void WasmStreamingObject::Finish(const FunctionCallbackInfo<Value>& args) { |
131 |
|
|
WasmStreamingObject* obj; |
132 |
✗✓ |
154 |
ASSIGN_OR_RETURN_UNWRAP(&obj, args.Holder()); |
133 |
✗✓ |
154 |
CHECK(obj->streaming_); |
134 |
|
|
|
135 |
✗✓ |
154 |
CHECK_EQ(args.Length(), 0); |
136 |
|
154 |
obj->streaming_->Finish(); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
74 |
void WasmStreamingObject::Abort(const FunctionCallbackInfo<Value>& args) { |
140 |
|
|
WasmStreamingObject* obj; |
141 |
✗✓ |
74 |
ASSIGN_OR_RETURN_UNWRAP(&obj, args.Holder()); |
142 |
✗✓ |
74 |
CHECK(obj->streaming_); |
143 |
|
|
|
144 |
✗✓ |
74 |
CHECK_EQ(args.Length(), 1); |
145 |
|
148 |
obj->streaming_->Abort(args[0]); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
228 |
void StartStreamingCompilation(const FunctionCallbackInfo<Value>& info) { |
149 |
|
|
// V8 passes an instance of v8::WasmStreaming to this callback, which we can |
150 |
|
|
// use to pass the WebAssembly module bytes to V8 as we receive them. |
151 |
|
|
// Unfortunately, our fetch() implementation is a JavaScript dependency, so it |
152 |
|
|
// is difficult to implement the required logic here. Instead, we create a |
153 |
|
|
// a WasmStreamingObject that encapsulates v8::WasmStreaming and that we can |
154 |
|
|
// pass to the JavaScript implementation. The JavaScript implementation can |
155 |
|
|
// then push() bytes from the Response and eventually either finish() or |
156 |
|
|
// abort() the operation. |
157 |
|
|
|
158 |
|
|
// Create the wrapper object. |
159 |
|
|
std::shared_ptr<WasmStreaming> streaming = |
160 |
|
228 |
WasmStreaming::Unpack(info.GetIsolate(), info.Data()); |
161 |
|
228 |
Environment* env = Environment::GetCurrent(info); |
162 |
|
|
Local<Object> obj; |
163 |
✗✓ |
456 |
if (!WasmStreamingObject::Create(env, streaming).ToLocal(&obj)) { |
164 |
|
|
// A JavaScript exception is pending. Let V8 deal with it. |
165 |
|
|
return; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
// V8 always passes one argument to this callback. |
169 |
✗✓ |
228 |
CHECK_EQ(info.Length(), 1); |
170 |
|
|
|
171 |
|
|
// Prepare the JavaScript implementation for invocation. We will pass the |
172 |
|
|
// WasmStreamingObject as the first argument, followed by the argument that we |
173 |
|
|
// received from V8, i.e., the first argument passed to compileStreaming (or |
174 |
|
|
// instantiateStreaming). |
175 |
|
228 |
Local<Function> impl = env->wasm_streaming_compilation_impl(); |
176 |
✗✓ |
228 |
CHECK(!impl.IsEmpty()); |
177 |
✓✗ |
456 |
Local<Value> args[] = {obj, info[0]}; |
178 |
|
|
|
179 |
|
|
// Hand control to the JavaScript implementation. It should never throw an |
180 |
|
|
// error, but if it does, we leave it to the calling V8 code to handle that |
181 |
|
|
// gracefully. Otherwise, we assert that the JavaScript function does not |
182 |
|
|
// return anything. |
183 |
|
|
MaybeLocal<Value> maybe_ret = |
184 |
|
456 |
impl->Call(env->context(), info.This(), 2, args); |
185 |
|
|
Local<Value> ret; |
186 |
✓✗✗✓ ✗✓ |
684 |
CHECK_IMPLIES(maybe_ret.ToLocal(&ret), ret->IsUndefined()); |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
// Called once by JavaScript during initialization. |
190 |
|
6055 |
void SetImplementation(const FunctionCallbackInfo<Value>& info) { |
191 |
|
6055 |
Environment* env = Environment::GetCurrent(info); |
192 |
|
12110 |
env->set_wasm_streaming_compilation_impl(info[0].As<Function>()); |
193 |
|
6055 |
} |
194 |
|
|
|
195 |
|
6055 |
void Initialize(Local<Object> target, |
196 |
|
|
Local<Value>, |
197 |
|
|
Local<Context> context, |
198 |
|
|
void*) { |
199 |
|
6055 |
SetMethod(context, target, "setImplementation", SetImplementation); |
200 |
|
6055 |
} |
201 |
|
|
|
202 |
|
5320 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
203 |
|
5320 |
registry->Register(SetImplementation); |
204 |
|
5320 |
registry->Register(StartStreamingCompilation); |
205 |
|
5320 |
WasmStreamingObject::RegisterExternalReferences(registry); |
206 |
|
5320 |
} |
207 |
|
|
|
208 |
|
|
} // namespace wasm_web_api |
209 |
|
|
} // namespace node |
210 |
|
|
|
211 |
|
5392 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(wasm_web_api, node::wasm_web_api::Initialize) |
212 |
|
5320 |
NODE_MODULE_EXTERNAL_REFERENCE(wasm_web_api, |
213 |
|
|
node::wasm_web_api::RegisterExternalReferences) |