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