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