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/assert-scope.h" | 7 #include "src/assert-scope.h" |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 #include "src/execution.h" | 10 #include "src/execution.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 namespace v8 { | 29 namespace v8 { |
30 | 30 |
31 namespace { | 31 namespace { |
32 struct RawBuffer { | 32 struct RawBuffer { |
33 const byte* start; | 33 const byte* start; |
34 const byte* end; | 34 const byte* end; |
35 size_t size() { return static_cast<size_t>(end - start); } | 35 size_t size() { return static_cast<size_t>(end - start); } |
36 }; | 36 }; |
37 | 37 |
38 RawBuffer GetRawBufferSource( | 38 RawBuffer GetRawBufferSource(v8::Local<v8::Value> source, |
39 v8::Local<v8::Value> source, ErrorThrower* thrower) { | 39 ErrorThrower* thrower) { |
40 const byte* start = nullptr; | 40 const byte* start = nullptr; |
41 const byte* end = nullptr; | 41 const byte* end = nullptr; |
42 | 42 |
43 if (source->IsArrayBuffer()) { | 43 if (source->IsArrayBuffer()) { |
44 // A raw array buffer was passed. | 44 // A raw array buffer was passed. |
45 Local<ArrayBuffer> buffer = Local<ArrayBuffer>::Cast(source); | 45 Local<ArrayBuffer> buffer = Local<ArrayBuffer>::Cast(source); |
46 ArrayBuffer::Contents contents = buffer->GetContents(); | 46 ArrayBuffer::Contents contents = buffer->GetContents(); |
47 | 47 |
48 start = reinterpret_cast<const byte*>(contents.Data()); | 48 start = reinterpret_cast<const byte*>(contents.Data()); |
49 end = start + contents.ByteLength(); | 49 end = start + contents.ByteLength(); |
50 | 50 |
51 if (start == nullptr || end == start) { | 51 if (start == nullptr || end == start) { |
52 thrower->Error("ArrayBuffer argument is empty"); | 52 thrower->Error("ArrayBuffer argument is empty"); |
53 } | 53 } |
54 } else if (source->IsTypedArray()) { | 54 } else if (source->IsTypedArray()) { |
55 // A TypedArray was passed. | 55 // A TypedArray was passed. |
56 Local<TypedArray> array = Local<TypedArray>::Cast(source); | 56 Local<TypedArray> array = Local<TypedArray>::Cast(source); |
57 Local<ArrayBuffer> buffer = array->Buffer(); | 57 Local<ArrayBuffer> buffer = array->Buffer(); |
58 | 58 |
59 ArrayBuffer::Contents contents = buffer->GetContents(); | 59 ArrayBuffer::Contents contents = buffer->GetContents(); |
60 | 60 |
61 start = | 61 start = |
62 reinterpret_cast<const byte*>(contents.Data()) + array->ByteOffset(); | 62 reinterpret_cast<const byte*>(contents.Data()) + array->ByteOffset(); |
63 end = start + array->ByteLength(); | 63 end = start + array->ByteLength(); |
64 | 64 |
65 if (start == nullptr || end == start) { | 65 if (start == nullptr || end == start) { |
66 thrower->Error("ArrayBuffer argument is empty"); | 66 thrower->Error("ArrayBuffer argument is empty"); |
67 } | 67 } |
68 } else { | 68 } else { |
69 thrower->Error("Argument 0 must be an ArrayBuffer or Uint8Array"); | 69 thrower->Error("Source must be an ArrayBuffer or Uint8Array"); |
70 } | 70 } |
71 | 71 |
72 return {start, end}; | 72 return {start, end}; |
73 } | 73 } |
74 | 74 |
| 75 i::Handle<i::JSArrayBuffer> GetMemoryArgument( |
| 76 const v8::FunctionCallbackInfo<v8::Value>& args, int index) { |
| 77 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); |
| 78 if (args.Length() > index && args[index]->IsArrayBuffer()) { |
| 79 Local<Object> obj = Local<Object>::Cast(args[index]); |
| 80 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); |
| 81 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); |
| 82 } |
| 83 return memory; |
| 84 } |
| 85 |
| 86 i::Handle<i::JSObject> GetForeignArgument( |
| 87 const v8::FunctionCallbackInfo<v8::Value>& args, int index) { |
| 88 i::Handle<i::JSObject> foreign; |
| 89 if (args.Length() > index && args[index]->IsObject()) { |
| 90 Local<Object> local_foreign = Local<Object>::Cast(args[index]); |
| 91 i::Handle<i::Object> foreign_obj = v8::Utils::OpenHandle(*local_foreign); |
| 92 foreign = i::Handle<i::JSObject>(i::JSObject::cast(*foreign_obj)); |
| 93 } |
| 94 return foreign; |
| 95 } |
| 96 |
75 void VerifyModule(const v8::FunctionCallbackInfo<v8::Value>& args) { | 97 void VerifyModule(const v8::FunctionCallbackInfo<v8::Value>& args) { |
76 HandleScope scope(args.GetIsolate()); | 98 HandleScope scope(args.GetIsolate()); |
77 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 99 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
78 ErrorThrower thrower(isolate, "Wasm.verifyModule()"); | 100 ErrorThrower thrower(isolate, "Wasm.verifyModule()"); |
79 | 101 |
80 if (args.Length() < 1) { | 102 if (args.Length() < 1) { |
81 thrower.Error("Argument 0 must be a buffer source"); | 103 thrower.Error("Argument 0 must be a buffer source"); |
82 return; | 104 return; |
83 } | 105 } |
84 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); | 106 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 buffer.start, buffer.end); | 139 buffer.start, buffer.end); |
118 } | 140 } |
119 | 141 |
120 if (result.failed()) { | 142 if (result.failed()) { |
121 thrower.Failed("", result); | 143 thrower.Failed("", result); |
122 } | 144 } |
123 | 145 |
124 if (result.val) delete result.val; | 146 if (result.val) delete result.val; |
125 } | 147 } |
126 | 148 |
127 v8::internal::wasm::ZoneBuffer* TranslateAsmModule( | 149 static bool ParseAsmModule(i::ParseInfo* info, ErrorThrower* thrower) { |
128 i::ParseInfo* info, ErrorThrower* thrower, | |
129 i::Handle<i::FixedArray>* foreign_args) { | |
130 info->set_global(); | 150 info->set_global(); |
131 info->set_lazy(false); | 151 info->set_lazy(false); |
132 info->set_allow_lazy_parsing(false); | 152 info->set_allow_lazy_parsing(false); |
133 info->set_toplevel(true); | 153 info->set_toplevel(true); |
134 | 154 |
135 if (!i::Compiler::ParseAndAnalyze(info)) { | 155 if (!i::Compiler::ParseAndAnalyze(info)) { |
136 return nullptr; | 156 return false; |
137 } | 157 } |
138 | 158 |
139 if (info->scope()->declarations()->length() == 0) { | 159 if (info->scope()->declarations()->length() == 0) { |
140 thrower->Error("Asm.js validation failed: no declarations in scope"); | 160 thrower->Error("Asm.js validation failed: no declarations in scope"); |
141 return nullptr; | 161 return false; |
142 } | 162 } |
143 | 163 |
144 info->set_literal( | 164 info->set_literal( |
145 info->scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); | 165 info->scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); |
146 | 166 return true; |
147 v8::internal::AsmTyper typer(info->isolate(), info->zone(), *(info->script()), | |
148 info->literal()); | |
149 if (i::FLAG_enable_simd_asmjs) { | |
150 typer.set_allow_simd(true); | |
151 } | |
152 if (!typer.Validate()) { | |
153 thrower->Error("Asm.js validation failed: %s", typer.error_message()); | |
154 return nullptr; | |
155 } | |
156 | |
157 v8::internal::wasm::AsmWasmBuilder builder(info->isolate(), info->zone(), | |
158 info->literal(), &typer); | |
159 | |
160 return builder.Run(foreign_args); | |
161 } | 167 } |
162 | 168 |
163 i::MaybeHandle<i::JSObject> InstantiateModuleCommon( | 169 i::MaybeHandle<i::JSObject> InstantiateModuleCommon( |
164 const v8::FunctionCallbackInfo<v8::Value>& args, const byte* start, | 170 i::Isolate* isolate, i::Handle<i::JSArrayBuffer> memory, |
165 const byte* end, ErrorThrower* thrower, | 171 i::Handle<i::JSObject> foreign, const byte* start, const byte* end, |
| 172 ErrorThrower* thrower, |
166 internal::wasm::ModuleOrigin origin = i::wasm::kWasmOrigin) { | 173 internal::wasm::ModuleOrigin origin = i::wasm::kWasmOrigin) { |
167 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | |
168 | |
169 // Decode but avoid a redundant pass over function bodies for verification. | 174 // Decode but avoid a redundant pass over function bodies for verification. |
170 // Verification will happen during compilation. | 175 // Verification will happen during compilation. |
171 i::Zone zone(isolate->allocator()); | 176 i::Zone zone(isolate->allocator()); |
172 internal::wasm::ModuleResult result = internal::wasm::DecodeWasmModule( | 177 internal::wasm::ModuleResult result = internal::wasm::DecodeWasmModule( |
173 isolate, &zone, start, end, false, origin); | 178 isolate, &zone, start, end, false, origin); |
174 | 179 |
175 i::MaybeHandle<i::JSObject> object; | 180 i::MaybeHandle<i::JSObject> object; |
176 if (result.failed() && origin == internal::wasm::kAsmJsOrigin) { | 181 if (result.failed() && origin == internal::wasm::kAsmJsOrigin) { |
177 thrower->Error("Asm.js converted module failed to decode"); | 182 thrower->Error("Asm.js converted module failed to decode"); |
178 } else if (result.failed()) { | 183 } else if (result.failed()) { |
179 thrower->Failed("", result); | 184 thrower->Failed("", result); |
180 } else { | 185 } else { |
181 // Success. Instantiate the module and return the object. | 186 // Success. Instantiate the module and return the object. |
182 i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null(); | 187 object = result.val->Instantiate(isolate, foreign, memory); |
183 if (args.Length() > 1 && args[1]->IsObject()) { | |
184 Local<Object> obj = Local<Object>::Cast(args[1]); | |
185 ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); | |
186 } | |
187 | |
188 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); | |
189 if (args.Length() > 2 && args[2]->IsArrayBuffer()) { | |
190 Local<Object> obj = Local<Object>::Cast(args[2]); | |
191 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); | |
192 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); | |
193 } | |
194 | |
195 object = result.val->Instantiate(isolate, ffi, memory); | |
196 if (!object.is_null()) { | |
197 args.GetReturnValue().Set(v8::Utils::ToLocal(object.ToHandleChecked())); | |
198 } | |
199 } | 188 } |
200 | 189 |
201 if (result.val) delete result.val; | 190 if (result.val) delete result.val; |
202 return object; | 191 return object; |
203 } | 192 } |
204 | 193 |
205 void InstantiateModuleFromAsm(const v8::FunctionCallbackInfo<v8::Value>& args) { | 194 void InstantiateModuleFromAsm(const v8::FunctionCallbackInfo<v8::Value>& args) { |
206 HandleScope scope(args.GetIsolate()); | 195 HandleScope scope(args.GetIsolate()); |
207 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 196 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
208 ErrorThrower thrower(isolate, "Wasm.instantiateModuleFromAsm()"); | 197 ErrorThrower thrower(isolate, "Wasm.instantiateModuleFromAsm()"); |
209 | 198 |
210 if (!args[0]->IsString()) { | 199 if (!args[0]->IsString()) { |
211 thrower.Error("Asm module text should be a string"); | 200 thrower.Error("Asm module text should be a string"); |
212 return; | 201 return; |
213 } | 202 } |
214 | 203 |
215 i::Factory* factory = isolate->factory(); | 204 i::Factory* factory = isolate->factory(); |
216 i::Zone zone(isolate->allocator()); | 205 i::Zone zone(isolate->allocator()); |
217 Local<String> source = Local<String>::Cast(args[0]); | 206 Local<String> source = Local<String>::Cast(args[0]); |
218 i::Handle<i::Script> script = factory->NewScript(Utils::OpenHandle(*source)); | 207 i::Handle<i::Script> script = factory->NewScript(Utils::OpenHandle(*source)); |
219 i::ParseInfo info(&zone, script); | 208 i::ParseInfo info(&zone, script); |
220 | 209 |
221 i::Handle<i::Object> foreign; | 210 if (!ParseAsmModule(&info, &thrower)) { |
222 if (args.Length() > 1 && args[1]->IsObject()) { | |
223 Local<Object> local_foreign = Local<Object>::Cast(args[1]); | |
224 foreign = v8::Utils::OpenHandle(*local_foreign); | |
225 } | |
226 | |
227 i::Handle<i::FixedArray> foreign_args; | |
228 auto module = TranslateAsmModule(&info, &thrower, &foreign_args); | |
229 if (module == nullptr) { | |
230 return; | 211 return; |
231 } | 212 } |
232 | 213 |
233 i::MaybeHandle<i::Object> maybe_module_object = | 214 i::Handle<i::FixedArray> wasm_data; |
234 InstantiateModuleCommon(args, module->begin(), module->end(), &thrower, | 215 if (!i::WasmJs::ConvertAsmToWasm(&info, false, &wasm_data)) { |
235 internal::wasm::kAsmJsOrigin); | 216 thrower.Error("Asm.js failed to validate"); |
236 if (maybe_module_object.is_null()) { | |
237 return; | 217 return; |
238 } | 218 } |
239 | 219 |
240 i::Handle<i::Name> name = | 220 i::Handle<i::JSObject> foreign = GetForeignArgument(args, 1); |
241 factory->NewStringFromStaticChars("__foreign_init__"); | 221 i::Handle<i::JSArrayBuffer> memory = GetMemoryArgument(args, 2); |
242 | 222 |
243 i::Handle<i::Object> module_object = maybe_module_object.ToHandleChecked(); | 223 i::Handle<i::Object> result; |
244 i::MaybeHandle<i::Object> maybe_init = | 224 if (!i::WasmJs::InstantiateAsmWasm(isolate, wasm_data, memory, foreign, |
245 i::Object::GetProperty(module_object, name); | 225 &result)) { |
246 DCHECK(!maybe_init.is_null()); | 226 return; |
| 227 } |
247 | 228 |
248 i::Handle<i::Object> init = maybe_init.ToHandleChecked(); | 229 if (!result.is_null()) { |
249 i::Handle<i::Object> undefined = isolate->factory()->undefined_value(); | 230 args.GetReturnValue().Set(v8::Utils::ToLocal(result)); |
250 i::Handle<i::Object>* foreign_args_array = | |
251 new i::Handle<i::Object>[foreign_args->length()]; | |
252 for (int j = 0; j < foreign_args->length(); j++) { | |
253 if (!foreign.is_null()) { | |
254 i::MaybeHandle<i::Name> name = i::Object::ToName( | |
255 isolate, i::Handle<i::Object>(foreign_args->get(j), isolate)); | |
256 if (!name.is_null()) { | |
257 i::MaybeHandle<i::Object> val = | |
258 i::Object::GetProperty(foreign, name.ToHandleChecked()); | |
259 if (!val.is_null()) { | |
260 foreign_args_array[j] = val.ToHandleChecked(); | |
261 continue; | |
262 } | |
263 } | |
264 } | |
265 foreign_args_array[j] = undefined; | |
266 } | |
267 i::MaybeHandle<i::Object> retval = i::Execution::Call( | |
268 isolate, init, undefined, foreign_args->length(), foreign_args_array); | |
269 delete[] foreign_args_array; | |
270 | |
271 if (retval.is_null()) { | |
272 thrower.Error( | |
273 "WASM.instantiateModuleFromAsm(): foreign init function failed"); | |
274 } | 231 } |
275 } | 232 } |
276 | 233 |
277 void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) { | 234 void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) { |
278 HandleScope scope(args.GetIsolate()); | 235 HandleScope scope(args.GetIsolate()); |
279 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 236 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
280 ErrorThrower thrower(isolate, "Wasm.instantiateModule()"); | 237 ErrorThrower thrower(isolate, "Wasm.instantiateModule()"); |
281 | 238 |
282 if (args.Length() < 1) { | 239 if (args.Length() < 1) { |
283 thrower.Error("Argument 0 must be a buffer source"); | 240 thrower.Error("Argument 0 must be a buffer source"); |
284 return; | 241 return; |
285 } | 242 } |
286 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); | 243 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); |
287 if (buffer.start == nullptr) return; | 244 if (buffer.start == nullptr) return; |
288 | 245 |
289 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower); | 246 i::Handle<i::JSObject> foreign = GetForeignArgument(args, 1); |
| 247 i::Handle<i::JSArrayBuffer> memory = GetMemoryArgument(args, 2); |
| 248 |
| 249 i::MaybeHandle<i::Object> result = InstantiateModuleCommon( |
| 250 isolate, memory, foreign, buffer.start, buffer.end, &thrower); |
| 251 |
| 252 if (!result.is_null()) { |
| 253 args.GetReturnValue().Set(v8::Utils::ToLocal(result.ToHandleChecked())); |
| 254 } |
290 } | 255 } |
291 | 256 |
292 | |
293 static i::MaybeHandle<i::JSObject> CreateModuleObject( | 257 static i::MaybeHandle<i::JSObject> CreateModuleObject( |
294 v8::Isolate* isolate, const v8::Local<v8::Value> source, | 258 v8::Isolate* isolate, const v8::Local<v8::Value> source, |
295 ErrorThrower* thrower) { | 259 ErrorThrower* thrower) { |
296 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 260 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
297 | 261 |
298 RawBuffer buffer = GetRawBufferSource(source, thrower); | 262 RawBuffer buffer = GetRawBufferSource(source, thrower); |
299 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>(); | 263 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>(); |
300 | 264 |
301 // TODO(rossberg): Once we can, do compilation here. | 265 // TODO(rossberg): Once we can, do compilation here. |
302 DCHECK(source->IsArrayBuffer() || source->IsTypedArray()); | 266 DCHECK(source->IsArrayBuffer() || source->IsTypedArray()); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 CreateModuleObject(isolate, args[0], &thrower); | 313 CreateModuleObject(isolate, args[0], &thrower); |
350 if (module_obj.is_null()) return; | 314 if (module_obj.is_null()) return; |
351 | 315 |
352 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); | 316 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); |
353 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked())); | 317 return_value.Set(Utils::ToLocal(module_obj.ToHandleChecked())); |
354 } | 318 } |
355 | 319 |
356 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { | 320 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { |
357 HandleScope scope(args.GetIsolate()); | 321 HandleScope scope(args.GetIsolate()); |
358 v8::Isolate* isolate = args.GetIsolate(); | 322 v8::Isolate* isolate = args.GetIsolate(); |
359 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate), | 323 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
360 "WebAssembly.Instance()"); | 324 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); |
361 | 325 |
362 if (args.Length() < 1) { | 326 if (args.Length() < 1) { |
363 thrower.Error("Argument 0 must be a WebAssembly.Module"); | 327 thrower.Error("Argument 0 must be a WebAssembly.Module"); |
364 return; | 328 return; |
365 } | 329 } |
366 Local<Context> context = isolate->GetCurrentContext(); | 330 Local<Context> context = isolate->GetCurrentContext(); |
367 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); | 331 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); |
368 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym()); | 332 i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym()); |
369 i::MaybeHandle<i::Object> source = | 333 i::MaybeHandle<i::Object> source = |
370 i::Object::GetProperty(Utils::OpenHandle(*args[0]), module_sym); | 334 i::Object::GetProperty(Utils::OpenHandle(*args[0]), module_sym); |
371 if (source.is_null()) return; | 335 if (source.is_null()) return; |
372 | 336 |
373 RawBuffer buffer = | 337 RawBuffer buffer = |
374 GetRawBufferSource(Utils::ToLocal(source.ToHandleChecked()), &thrower); | 338 GetRawBufferSource(Utils::ToLocal(source.ToHandleChecked()), &thrower); |
375 if (buffer.start == nullptr) return; | 339 if (buffer.start == nullptr) return; |
376 | 340 |
377 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower); | 341 i::Handle<i::JSObject> foreign = GetForeignArgument(args, 1); |
| 342 i::Handle<i::JSArrayBuffer> memory = GetMemoryArgument(args, 2); |
| 343 |
| 344 i::MaybeHandle<i::Object> result = InstantiateModuleCommon( |
| 345 i_isolate, memory, foreign, buffer.start, buffer.end, &thrower); |
| 346 |
| 347 if (!result.is_null()) { |
| 348 args.GetReturnValue().Set(v8::Utils::ToLocal(result.ToHandleChecked())); |
| 349 } |
378 } | 350 } |
379 } // namespace | 351 } // namespace |
380 | 352 |
381 // TODO(titzer): we use the API to create the function template because the | 353 // TODO(titzer): we use the API to create the function template because the |
382 // internal guts are too ugly to replicate here. | 354 // internal guts are too ugly to replicate here. |
383 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate, | 355 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate, |
384 FunctionCallback func) { | 356 FunctionCallback func) { |
385 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate); | 357 Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate); |
386 Local<FunctionTemplate> local = FunctionTemplate::New(isolate, func); | 358 Local<FunctionTemplate> local = FunctionTemplate::New(isolate, func); |
387 return v8::Utils::OpenHandle(*local); | 359 return v8::Utils::OpenHandle(*local); |
(...skipping 10 matching lines...) Expand all Loading... |
398 Handle<FunctionTemplateInfo> temp = NewTemplate(isolate, func); | 370 Handle<FunctionTemplateInfo> temp = NewTemplate(isolate, func); |
399 Handle<JSFunction> function = | 371 Handle<JSFunction> function = |
400 ApiNatives::InstantiateFunction(temp).ToHandleChecked(); | 372 ApiNatives::InstantiateFunction(temp).ToHandleChecked(); |
401 PropertyAttributes attributes = | 373 PropertyAttributes attributes = |
402 static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); | 374 static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
403 JSObject::AddProperty(object, name, function, attributes); | 375 JSObject::AddProperty(object, name, function, attributes); |
404 return function; | 376 return function; |
405 } | 377 } |
406 | 378 |
407 void WasmJs::Install(Isolate* isolate, Handle<JSGlobalObject> global) { | 379 void WasmJs::Install(Isolate* isolate, Handle<JSGlobalObject> global) { |
| 380 if (!FLAG_expose_wasm && !FLAG_validate_asm) { |
| 381 return; |
| 382 } |
408 Factory* factory = isolate->factory(); | 383 Factory* factory = isolate->factory(); |
409 | 384 |
410 // Setup wasm function map. | 385 // Setup wasm function map. |
411 Handle<Context> context(global->native_context(), isolate); | 386 Handle<Context> context(global->native_context(), isolate); |
412 InstallWasmFunctionMap(isolate, context); | 387 InstallWasmFunctionMap(isolate, context); |
413 | 388 |
| 389 if (!FLAG_expose_wasm) { |
| 390 return; |
| 391 } |
| 392 |
414 // Bind the experimental WASM object. | 393 // Bind the experimental WASM object. |
415 // TODO(rossberg, titzer): remove once it's no longer needed. | 394 // TODO(rossberg, titzer): remove once it's no longer needed. |
416 { | 395 { |
417 Handle<String> name = v8_str(isolate, "Wasm"); | 396 Handle<String> name = v8_str(isolate, "Wasm"); |
418 Handle<JSFunction> cons = factory->NewFunction(name); | 397 Handle<JSFunction> cons = factory->NewFunction(name); |
419 JSFunction::SetInstancePrototype( | 398 JSFunction::SetInstancePrototype( |
420 cons, Handle<Object>(context->initial_object_prototype(), isolate)); | 399 cons, Handle<Object>(context->initial_object_prototype(), isolate)); |
421 cons->shared()->set_instance_class_name(*name); | 400 cons->shared()->set_instance_class_name(*name); |
422 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED); | 401 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED); |
423 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM); | 402 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 &in_object_properties); | 464 &in_object_properties); |
486 | 465 |
487 int unused_property_fields = in_object_properties - pre_allocated; | 466 int unused_property_fields = in_object_properties - pre_allocated; |
488 Handle<Map> map = Map::CopyInitialMap( | 467 Handle<Map> map = Map::CopyInitialMap( |
489 prev_map, instance_size, in_object_properties, unused_property_fields); | 468 prev_map, instance_size, in_object_properties, unused_property_fields); |
490 | 469 |
491 context->set_wasm_function_map(*map); | 470 context->set_wasm_function_map(*map); |
492 } | 471 } |
493 } | 472 } |
494 | 473 |
| 474 bool WasmJs::ConvertAsmToWasm(ParseInfo* info, bool fixed_signature, |
| 475 Handle<FixedArray>* wasm_data) { |
| 476 ErrorThrower thrower(info->isolate(), "Asm.js -> WebAssembly conversion"); |
| 477 AsmTyper typer(info->isolate(), info->zone(), *(info->script()), |
| 478 info->literal()); |
| 479 typer.set_fixed_signature(fixed_signature); |
| 480 if (i::FLAG_enable_simd_asmjs) { |
| 481 typer.set_allow_simd(true); |
| 482 } |
| 483 if (!typer.Validate()) { |
| 484 DCHECK(!info->isolate()->has_pending_exception()); |
| 485 PrintF("Validation of asm.js module failed: %s", typer.error_message()); |
| 486 return false; |
| 487 } |
| 488 v8::internal::wasm::AsmWasmBuilder builder(info->isolate(), info->zone(), |
| 489 info->literal(), &typer); |
| 490 i::Handle<i::FixedArray> foreign_globals; |
| 491 auto module = builder.Run(&foreign_globals); |
| 492 size_t byte_length = module->end() - module->begin(); |
| 493 Handle<JSArrayBuffer> buffer = info->isolate()->factory()->NewJSArrayBuffer(); |
| 494 JSArrayBuffer::SetupAllocatingData(buffer, info->isolate(), byte_length, |
| 495 false, SharedFlag::kNotShared); |
| 496 uint8_t* module_bytes = reinterpret_cast<uint8_t*>(buffer->backing_store()); |
| 497 memcpy(module_bytes, module->begin(), byte_length); |
| 498 *wasm_data = info->isolate()->factory()->NewFixedArray(2); |
| 499 (*wasm_data)->set(0, *buffer); |
| 500 (*wasm_data)->set(1, *foreign_globals); |
| 501 return true; |
| 502 } |
| 503 |
| 504 bool WasmJs::InstantiateAsmWasm(i::Isolate* isolate, |
| 505 Handle<FixedArray> wasm_data, |
| 506 Handle<JSArrayBuffer> memory, |
| 507 Handle<JSObject> foreign, |
| 508 Handle<Object>* result) { |
| 509 i::Handle<i::JSArrayBuffer> module_bytes( |
| 510 i::JSArrayBuffer::cast(wasm_data->get(0))); |
| 511 i::Handle<i::FixedArray> foreign_globals( |
| 512 i::FixedArray::cast(wasm_data->get(1))); |
| 513 |
| 514 ErrorThrower thrower(isolate, "Asm.js -> WebAssembly instantiation"); |
| 515 i::Factory* factory = isolate->factory(); |
| 516 |
| 517 const byte* module_start = |
| 518 reinterpret_cast<const byte*>(module_bytes->backing_store()); |
| 519 size_t module_length = |
| 520 static_cast<size_t>(module_bytes->byte_length()->Number()); |
| 521 const byte* module_end = module_start + module_length; |
| 522 i::MaybeHandle<i::JSObject> maybe_module_object = InstantiateModuleCommon( |
| 523 isolate, memory, foreign, module_start, module_end, &thrower, |
| 524 internal::wasm::kAsmJsOrigin); |
| 525 if (maybe_module_object.is_null()) { |
| 526 return false; |
| 527 } |
| 528 |
| 529 i::Handle<i::Name> name = |
| 530 factory->NewStringFromStaticChars("__foreign_init__"); |
| 531 |
| 532 i::Handle<i::Object> module_object = maybe_module_object.ToHandleChecked(); |
| 533 i::MaybeHandle<i::Object> maybe_init = |
| 534 i::Object::GetProperty(module_object, name); |
| 535 DCHECK(!maybe_init.is_null()); |
| 536 |
| 537 i::Handle<i::Object> init = maybe_init.ToHandleChecked(); |
| 538 i::Handle<i::Object> undefined(isolate->heap()->undefined_value(), isolate); |
| 539 i::Handle<i::Object>* foreign_args_array = |
| 540 new i::Handle<i::Object>[foreign_globals->length()]; |
| 541 for (int j = 0; j < foreign_globals->length(); j++) { |
| 542 if (!foreign.is_null()) { |
| 543 i::MaybeHandle<i::Name> name = i::Object::ToName( |
| 544 isolate, i::Handle<i::Object>(foreign_globals->get(j), isolate)); |
| 545 if (!name.is_null()) { |
| 546 i::MaybeHandle<i::Object> val = |
| 547 i::Object::GetProperty(foreign, name.ToHandleChecked()); |
| 548 if (!val.is_null()) { |
| 549 foreign_args_array[j] = val.ToHandleChecked(); |
| 550 continue; |
| 551 } |
| 552 } |
| 553 } |
| 554 foreign_args_array[j] = undefined; |
| 555 } |
| 556 i::MaybeHandle<i::Object> retval = i::Execution::Call( |
| 557 isolate, init, undefined, foreign_globals->length(), foreign_args_array); |
| 558 delete[] foreign_args_array; |
| 559 |
| 560 if (retval.is_null()) { |
| 561 thrower.Error( |
| 562 "WASM.instantiateModuleFromAsm(): foreign init function failed"); |
| 563 return false; |
| 564 } else { |
| 565 *result = maybe_module_object.ToHandleChecked(); |
| 566 } |
| 567 |
| 568 return true; |
| 569 } |
| 570 |
495 } // namespace internal | 571 } // namespace internal |
496 } // namespace v8 | 572 } // namespace v8 |
OLD | NEW |