OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/api-natives.h" | 5 #include "src/api-natives.h" |
6 #include "src/api.h" | 6 #include "src/api.h" |
7 #include "src/asmjs/asm-js.h" | 7 #include "src/asmjs/asm-js.h" |
8 #include "src/asmjs/asm-typer.h" | 8 #include "src/asmjs/asm-typer.h" |
9 #include "src/asmjs/asm-wasm-builder.h" | 9 #include "src/asmjs/asm-wasm-builder.h" |
10 #include "src/assert-scope.h" | 10 #include "src/assert-scope.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 buffer.start, buffer.end); | 118 buffer.start, buffer.end); |
119 } | 119 } |
120 | 120 |
121 if (result.failed()) { | 121 if (result.failed()) { |
122 thrower.Failed("", result); | 122 thrower.Failed("", result); |
123 } | 123 } |
124 | 124 |
125 if (result.val) delete result.val; | 125 if (result.val) delete result.val; |
126 } | 126 } |
127 | 127 |
128 i::MaybeHandle<i::FixedArray> TranslateAsmModule(i::ParseInfo* info, | |
129 ErrorThrower* thrower) { | |
130 info->set_global(); | |
131 info->set_lazy(false); | |
132 info->set_allow_lazy_parsing(false); | |
133 info->set_toplevel(true); | |
134 | |
135 if (!i::Compiler::ParseAndAnalyze(info)) { | |
136 return i::MaybeHandle<i::FixedArray>(); | |
137 } | |
138 | |
139 if (info->scope()->declarations()->length() == 0) { | |
140 thrower->Error("Asm.js validation failed: no declarations in scope"); | |
141 return i::MaybeHandle<i::FixedArray>(); | |
142 } | |
143 | |
144 if (!info->scope()->declarations()->at(0)->IsFunctionDeclaration()) { | |
145 thrower->Error("Asm.js validation failed: non-function declaration"); | |
146 return i::MaybeHandle<i::FixedArray>(); | |
147 } | |
148 | |
149 info->set_literal( | |
150 info->scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); | |
151 | |
152 return i::AsmJs::ConvertAsmToWasm(info); | |
153 } | |
154 | |
155 i::MaybeHandle<i::JSObject> InstantiateModule( | 128 i::MaybeHandle<i::JSObject> InstantiateModule( |
156 const v8::FunctionCallbackInfo<v8::Value>& args, const byte* start, | 129 const v8::FunctionCallbackInfo<v8::Value>& args, const byte* start, |
157 const byte* end, ErrorThrower* thrower, | 130 const byte* end, ErrorThrower* thrower, |
158 internal::wasm::ModuleOrigin origin = i::wasm::kWasmOrigin) { | 131 internal::wasm::ModuleOrigin origin = i::wasm::kWasmOrigin) { |
159 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 132 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
160 | 133 |
161 // Decode but avoid a redundant pass over function bodies for verification. | 134 // Decode but avoid a redundant pass over function bodies for verification. |
162 // Verification will happen during compilation. | 135 // Verification will happen during compilation. |
163 i::Zone zone(isolate->allocator()); | 136 i::Zone zone(isolate->allocator()); |
164 internal::wasm::ModuleResult result = internal::wasm::DecodeWasmModule( | 137 internal::wasm::ModuleResult result = internal::wasm::DecodeWasmModule( |
(...skipping 26 matching lines...) Expand all Loading... |
191 if (!object.is_null()) { | 164 if (!object.is_null()) { |
192 args.GetReturnValue().Set(v8::Utils::ToLocal(object.ToHandleChecked())); | 165 args.GetReturnValue().Set(v8::Utils::ToLocal(object.ToHandleChecked())); |
193 } | 166 } |
194 } | 167 } |
195 } | 168 } |
196 | 169 |
197 if (result.val) delete result.val; | 170 if (result.val) delete result.val; |
198 return object; | 171 return object; |
199 } | 172 } |
200 | 173 |
201 void InstantiateModuleFromAsm(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
202 HandleScope scope(args.GetIsolate()); | |
203 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | |
204 ErrorThrower thrower(isolate, "Wasm.instantiateModuleFromAsm()"); | |
205 | |
206 if (!args[0]->IsString()) { | |
207 thrower.Error("Asm module text should be a string"); | |
208 return; | |
209 } | |
210 | |
211 i::Factory* factory = isolate->factory(); | |
212 i::Zone zone(isolate->allocator()); | |
213 Local<String> source = Local<String>::Cast(args[0]); | |
214 i::Handle<i::Script> script = factory->NewScript(Utils::OpenHandle(*source)); | |
215 i::ParseInfo info(&zone, script); | |
216 | |
217 auto wasm_data = TranslateAsmModule(&info, &thrower); | |
218 if (wasm_data.is_null()) { | |
219 thrower.Error("asm.js failed to validate"); | |
220 return; | |
221 } | |
222 | |
223 i::Handle<i::JSReceiver> stdlib; | |
224 if (args.Length() > 1 && args[1]->IsObject()) { | |
225 Local<Object> obj = Local<Object>::Cast(args[1]); | |
226 i::Handle<i::Object> hobj = | |
227 i::Handle<i::Object>::cast(v8::Utils::OpenHandle(*obj)); | |
228 if (hobj->IsJSReceiver()) { | |
229 stdlib = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); | |
230 } | |
231 } | |
232 | |
233 i::Handle<i::JSReceiver> foreign; | |
234 if (args.Length() > 2 && args[2]->IsObject()) { | |
235 Local<Object> obj = Local<Object>::Cast(args[2]); | |
236 i::Handle<i::Object> hobj = | |
237 i::Handle<i::Object>::cast(v8::Utils::OpenHandle(*obj)); | |
238 if (hobj->IsJSReceiver()) { | |
239 foreign = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); | |
240 } | |
241 } | |
242 | |
243 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); | |
244 if (args.Length() > 3 && args[3]->IsArrayBuffer()) { | |
245 Local<Object> obj = Local<Object>::Cast(args[3]); | |
246 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); | |
247 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); | |
248 } | |
249 | |
250 if (!i::AsmJs::IsStdlibValid(isolate, wasm_data.ToHandleChecked(), stdlib)) { | |
251 thrower.Error("Asm module uses missing stdlib function"); | |
252 return; | |
253 } | |
254 | |
255 i::MaybeHandle<i::Object> maybe_module_object = i::AsmJs::InstantiateAsmWasm( | |
256 isolate, wasm_data.ToHandleChecked(), memory, foreign); | |
257 if (maybe_module_object.is_null()) { | |
258 return; | |
259 } | |
260 | |
261 args.GetReturnValue().Set( | |
262 v8::Utils::ToLocal(maybe_module_object.ToHandleChecked())); | |
263 } | |
264 | |
265 void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) { | 174 void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) { |
266 HandleScope scope(args.GetIsolate()); | 175 HandleScope scope(args.GetIsolate()); |
267 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 176 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
268 ErrorThrower thrower(isolate, "Wasm.instantiateModule()"); | 177 ErrorThrower thrower(isolate, "Wasm.instantiateModule()"); |
269 | 178 |
270 if (args.Length() < 1) { | 179 if (args.Length() < 1) { |
271 thrower.Error("Argument 0 must be a buffer source"); | 180 thrower.Error("Argument 0 must be a buffer source"); |
272 return; | 181 return; |
273 } | 182 } |
274 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); | 183 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 cons, Handle<Object>(context->initial_object_prototype(), isolate)); | 364 cons, Handle<Object>(context->initial_object_prototype(), isolate)); |
456 cons->shared()->set_instance_class_name(*name); | 365 cons->shared()->set_instance_class_name(*name); |
457 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED); | 366 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED); |
458 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM); | 367 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM); |
459 JSObject::AddProperty(global, name, wasm_object, attributes); | 368 JSObject::AddProperty(global, name, wasm_object, attributes); |
460 | 369 |
461 // Install functions on the WASM object. | 370 // Install functions on the WASM object. |
462 InstallFunc(isolate, wasm_object, "verifyModule", VerifyModule); | 371 InstallFunc(isolate, wasm_object, "verifyModule", VerifyModule); |
463 InstallFunc(isolate, wasm_object, "verifyFunction", VerifyFunction); | 372 InstallFunc(isolate, wasm_object, "verifyFunction", VerifyFunction); |
464 InstallFunc(isolate, wasm_object, "instantiateModule", InstantiateModule); | 373 InstallFunc(isolate, wasm_object, "instantiateModule", InstantiateModule); |
465 InstallFunc(isolate, wasm_object, "instantiateModuleFromAsm", | |
466 InstantiateModuleFromAsm); | |
467 | 374 |
468 { | 375 { |
469 // Add the Wasm.experimentalVersion property. | 376 // Add the Wasm.experimentalVersion property. |
470 Handle<String> name = v8_str(isolate, "experimentalVersion"); | 377 Handle<String> name = v8_str(isolate, "experimentalVersion"); |
471 PropertyAttributes attributes = | 378 PropertyAttributes attributes = |
472 static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); | 379 static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
473 Handle<Smi> value = | 380 Handle<Smi> value = |
474 Handle<Smi>(Smi::FromInt(wasm::kWasmVersion), isolate); | 381 Handle<Smi>(Smi::FromInt(wasm::kWasmVersion), isolate); |
475 JSObject::AddProperty(wasm_object, name, value, attributes); | 382 JSObject::AddProperty(wasm_object, name, value, attributes); |
476 } | 383 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 int unused_property_fields = in_object_properties - pre_allocated; | 437 int unused_property_fields = in_object_properties - pre_allocated; |
531 Handle<Map> map = Map::CopyInitialMap( | 438 Handle<Map> map = Map::CopyInitialMap( |
532 prev_map, instance_size, in_object_properties, unused_property_fields); | 439 prev_map, instance_size, in_object_properties, unused_property_fields); |
533 | 440 |
534 context->set_wasm_function_map(*map); | 441 context->set_wasm_function_map(*map); |
535 } | 442 } |
536 } | 443 } |
537 | 444 |
538 } // namespace internal | 445 } // namespace internal |
539 } // namespace v8 | 446 } // namespace v8 |
OLD | NEW |