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