Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: src/wasm/wasm-js.cc

Issue 2384513002: [wasm] Implement WebAssembly.validate() (Closed)
Patch Set: Address review comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/wasm/wasm-module.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 190
191 RawBuffer buffer = GetRawBufferSource(source, thrower); 191 RawBuffer buffer = GetRawBufferSource(source, thrower);
192 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>(); 192 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>();
193 193
194 DCHECK(source->IsArrayBuffer() || source->IsTypedArray()); 194 DCHECK(source->IsArrayBuffer() || source->IsTypedArray());
195 return i::wasm::CreateModuleObjectFromBytes( 195 return i::wasm::CreateModuleObjectFromBytes(
196 i_isolate, buffer.start, buffer.end, thrower, 196 i_isolate, buffer.start, buffer.end, thrower,
197 i::wasm::ModuleOrigin::kWasmOrigin); 197 i::wasm::ModuleOrigin::kWasmOrigin);
198 } 198 }
199 199
200 static bool ValidateModule(v8::Isolate* isolate,
201 const v8::Local<v8::Value> source,
202 ErrorThrower* thrower) {
203 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
204 i::MaybeHandle<i::JSObject> nothing;
205
206 RawBuffer buffer = GetRawBufferSource(source, thrower);
207 if (buffer.start == nullptr) return false;
208
209 DCHECK(source->IsArrayBuffer() || source->IsTypedArray());
210 return i::wasm::ValidateModuleBytes(i_isolate, buffer.start, buffer.end,
211 thrower,
212 i::wasm::ModuleOrigin::kWasmOrigin);
213 }
214
200 bool BrandCheck(Isolate* isolate, i::Handle<i::Object> value, 215 bool BrandCheck(Isolate* isolate, i::Handle<i::Object> value,
201 i::Handle<i::Symbol> sym, const char* msg) { 216 i::Handle<i::Symbol> sym, const char* msg) {
202 if (value->IsJSObject()) { 217 if (value->IsJSObject()) {
203 i::Handle<i::JSObject> object = i::Handle<i::JSObject>::cast(value); 218 i::Handle<i::JSObject> object = i::Handle<i::JSObject>::cast(value);
204 Maybe<bool> has_brand = i::JSObject::HasOwnProperty(object, sym); 219 Maybe<bool> has_brand = i::JSObject::HasOwnProperty(object, sym);
205 if (has_brand.IsNothing()) return false; 220 if (has_brand.IsNothing()) return false;
206 if (has_brand.ToChecked()) return true; 221 if (has_brand.ToChecked()) return true;
207 } 222 }
208 v8::Local<v8::Value> e = v8::Exception::TypeError(v8_str(isolate, msg)); 223 v8::Local<v8::Value> e = v8::Exception::TypeError(v8_str(isolate, msg));
209 isolate->ThrowException(e); 224 isolate->ThrowException(e);
(...skipping 18 matching lines...) Expand all
228 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) return; 243 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) return;
229 if (thrower.error()) { 244 if (thrower.error()) {
230 resolver->Reject(context, Utils::ToLocal(thrower.Reify())); 245 resolver->Reject(context, Utils::ToLocal(thrower.Reify()));
231 } else { 246 } else {
232 resolver->Resolve(context, Utils::ToLocal(module_obj.ToHandleChecked())); 247 resolver->Resolve(context, Utils::ToLocal(module_obj.ToHandleChecked()));
233 } 248 }
234 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 249 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
235 return_value.Set(resolver->GetPromise()); 250 return_value.Set(resolver->GetPromise());
236 } 251 }
237 252
253 void WebAssemblyValidate(const v8::FunctionCallbackInfo<v8::Value>& args) {
254 v8::Isolate* isolate = args.GetIsolate();
255 HandleScope scope(isolate);
256 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate),
257 "WebAssembly.validate()");
258
259 if (args.Length() < 1) {
260 thrower.TypeError("Argument 0 must be a buffer source");
261 return;
262 }
263
264 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
265 if (ValidateModule(isolate, args[0], &thrower)) {
266 return_value.Set(v8::True(isolate));
267 } else {
268 return_value.Set(v8::False(isolate));
269 }
270 }
271
238 void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) { 272 void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
239 v8::Isolate* isolate = args.GetIsolate(); 273 v8::Isolate* isolate = args.GetIsolate();
240 HandleScope scope(isolate); 274 HandleScope scope(isolate);
241 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate), 275 ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate),
242 "WebAssembly.Module()"); 276 "WebAssembly.Module()");
243 277
244 if (args.Length() < 1) { 278 if (args.Length() < 1) {
245 thrower.TypeError("Argument 0 must be a buffer source"); 279 thrower.TypeError("Argument 0 must be a buffer source");
246 return; 280 return;
247 } 281 }
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 JSFunction::SetInstancePrototype( 605 JSFunction::SetInstancePrototype(
572 cons, Handle<Object>(context->initial_object_prototype(), isolate)); 606 cons, Handle<Object>(context->initial_object_prototype(), isolate));
573 cons->shared()->set_instance_class_name(*name); 607 cons->shared()->set_instance_class_name(*name);
574 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED); 608 Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED);
575 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM); 609 PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM);
576 JSObject::AddProperty(global, name, wasm_object, attributes); 610 JSObject::AddProperty(global, name, wasm_object, attributes);
577 611
578 // Setup compile 612 // Setup compile
579 InstallFunc(isolate, wasm_object, "compile", WebAssemblyCompile); 613 InstallFunc(isolate, wasm_object, "compile", WebAssemblyCompile);
580 614
615 // Setup compile
616 InstallFunc(isolate, wasm_object, "validate", WebAssemblyValidate);
617
581 // Setup Module 618 // Setup Module
582 Handle<JSFunction> module_constructor = 619 Handle<JSFunction> module_constructor =
583 InstallFunc(isolate, wasm_object, "Module", WebAssemblyModule); 620 InstallFunc(isolate, wasm_object, "Module", WebAssemblyModule);
584 context->set_wasm_module_constructor(*module_constructor); 621 context->set_wasm_module_constructor(*module_constructor);
585 Handle<JSObject> module_proto = 622 Handle<JSObject> module_proto =
586 factory->NewJSObject(module_constructor, TENURED); 623 factory->NewJSObject(module_constructor, TENURED);
587 i::Handle<i::Map> map = isolate->factory()->NewMap( 624 i::Handle<i::Map> map = isolate->factory()->NewMap(
588 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + i::kPointerSize); 625 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + i::kPointerSize);
589 JSFunction::SetInitialMap(module_constructor, map, module_proto); 626 JSFunction::SetInitialMap(module_constructor, map, module_proto);
590 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(), 627 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(),
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 int unused_property_fields = in_object_properties - pre_allocated; 732 int unused_property_fields = in_object_properties - pre_allocated;
696 Handle<Map> map = Map::CopyInitialMap( 733 Handle<Map> map = Map::CopyInitialMap(
697 prev_map, instance_size, in_object_properties, unused_property_fields); 734 prev_map, instance_size, in_object_properties, unused_property_fields);
698 735
699 context->set_wasm_function_map(*map); 736 context->set_wasm_function_map(*map);
700 } 737 }
701 } 738 }
702 739
703 } // namespace internal 740 } // namespace internal
704 } // namespace v8 741 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/wasm/wasm-module.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698