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