Skip to content

Commit

Permalink
Bug 1310239 - Baldr: include field name in error message (r=bbouvier)
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 4kQJsrrTSH0
  • Loading branch information
Luke Wagner committed Oct 17, 2016
1 parent 6df83e1 commit 5c449cb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
18 changes: 9 additions & 9 deletions js/src/asmjs/WasmJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ ThrowBadImportArg(JSContext* cx)
}

static bool
ThrowBadImportField(JSContext* cx, const char* str)
ThrowBadImportField(JSContext* cx, const char* field, const char* str)
{
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_WASM_BAD_IMPORT_FIELD, str);
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_WASM_BAD_IMPORT_FIELD, field, str);
return false;
}

Expand Down Expand Up @@ -233,7 +233,7 @@ GetImports(JSContext* cx,
return false;

if (!v.isObject())
return ThrowBadImportField(cx, "an Object");
return ThrowBadImportField(cx, import.module.get(), "an Object");

RootedObject obj(cx, &v.toObject());
if (!GetProperty(cx, obj, import.field.get(), &v))
Expand All @@ -242,22 +242,22 @@ GetImports(JSContext* cx,
switch (import.kind) {
case DefinitionKind::Function:
if (!IsFunctionObject(v))
return ThrowBadImportField(cx, "a Function");
return ThrowBadImportField(cx, import.field.get(), "a Function");

if (!funcImports.append(&v.toObject().as<JSFunction>()))
return false;

break;
case DefinitionKind::Table:
if (!v.isObject() || !v.toObject().is<WasmTableObject>())
return ThrowBadImportField(cx, "a Table");
return ThrowBadImportField(cx, import.field.get(), "a Table");

MOZ_ASSERT(!tableImport);
tableImport.set(&v.toObject().as<WasmTableObject>());
break;
case DefinitionKind::Memory:
if (!v.isObject() || !v.toObject().is<WasmMemoryObject>())
return ThrowBadImportField(cx, "a Memory");
return ThrowBadImportField(cx, import.field.get(), "a Memory");

MOZ_ASSERT(!memoryImport);
memoryImport.set(&v.toObject().as<WasmMemoryObject>());
Expand All @@ -271,7 +271,7 @@ GetImports(JSContext* cx,
switch (global.type()) {
case ValType::I32: {
if (!v.isNumber())
return ThrowBadImportField(cx, "a number");
return ThrowBadImportField(cx, import.field.get(), "a number");
int32_t i32;
if (!ToInt32(cx, v, &i32))
return false;
Expand All @@ -295,7 +295,7 @@ GetImports(JSContext* cx,
break;
}
if (!v.isNumber())
return ThrowBadImportField(cx, "a number");
return ThrowBadImportField(cx, import.field.get(), "a number");
double d;
if (!ToNumber(cx, v, &d))
return false;
Expand All @@ -311,7 +311,7 @@ GetImports(JSContext* cx,
break;
}
if (!v.isNumber())
return ThrowBadImportField(cx, "a number");
return ThrowBadImportField(cx, import.field.get(), "a number");
double d;
if (!ToNumber(cx, v, &d))
return false;
Expand Down
4 changes: 2 additions & 2 deletions js/src/jit-test/tests/wasm/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ const noImportObj = "second argument must be an object";
assertErrorMessage(() => wasmEvalText('(module (import "a" "b"))', 1), TypeError, noImportObj);
assertErrorMessage(() => wasmEvalText('(module (import "a" "b"))', null), TypeError, noImportObj);

const notObject = /import object field is not an Object/;
const notFunction = /import object field is not a Function/;
const notObject = /import object field '\w*' is not an Object/;
const notFunction = /import object field '\w*' is not a Function/;

var code = '(module (import "a" "b"))';
assertErrorMessage(() => wasmEvalText(code), TypeError, noImportObj);
Expand Down
32 changes: 16 additions & 16 deletions js/src/jit-test/tests/wasm/import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ assertErrorMessage(() => new Memory({initial:2, maximum:1}), RangeError, /bad Me

const m1 = new Module(wasmTextToBinary('(module (import "foo" "bar") (import "baz" "quux"))'));
assertErrorMessage(() => new Instance(m1), TypeError, /second argument must be an object/);
assertErrorMessage(() => new Instance(m1, {foo:null}), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m1, {foo:{bar:{}}}), TypeError, /import object field is not a Function/);
assertErrorMessage(() => new Instance(m1, {foo:{bar:()=>{}}, baz:null}), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m1, {foo:{bar:()=>{}}, baz:{}}), TypeError, /import object field is not a Function/);
assertErrorMessage(() => new Instance(m1, {foo:null}), TypeError, /import object field 'foo' is not an Object/);
assertErrorMessage(() => new Instance(m1, {foo:{bar:{}}}), TypeError, /import object field 'bar' is not a Function/);
assertErrorMessage(() => new Instance(m1, {foo:{bar:()=>{}}, baz:null}), TypeError, /import object field 'baz' is not an Object/);
assertErrorMessage(() => new Instance(m1, {foo:{bar:()=>{}}, baz:{}}), TypeError, /import object field 'quux' is not a Function/);
assertEq(new Instance(m1, {foo:{bar:()=>{}}, baz:{quux:()=>{}}}) instanceof Instance, true);

const m2 = new Module(wasmTextToBinary('(module (import "x" "y" (memory 2 3)))'));
assertErrorMessage(() => new Instance(m2), TypeError, /second argument must be an object/);
assertErrorMessage(() => new Instance(m2, {x:null}), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m2, {x:{y:{}}}), TypeError, /import object field is not a Memory/);
assertErrorMessage(() => new Instance(m2, {x:null}), TypeError, /import object field 'x' is not an Object/);
assertErrorMessage(() => new Instance(m2, {x:{y:{}}}), TypeError, /import object field 'y' is not a Memory/);
assertErrorMessage(() => new Instance(m2, {x:{y:mem1Page}}), TypeError, /imported Memory with incompatible size/);
assertErrorMessage(() => new Instance(m2, {x:{y:mem1PageMax1}}), TypeError, /imported Memory with incompatible size/);
assertErrorMessage(() => new Instance(m2, {x:{y:mem4Page}}), TypeError, /imported Memory with incompatible size/);
Expand All @@ -48,19 +48,19 @@ assertErrorMessage(() => new Instance(m2, {x:{y:mem2PageMax4}}), TypeError, /imp

const m3 = new Module(wasmTextToBinary('(module (import "foo" "bar" (memory 1 1)) (import "baz" "quux"))'));
assertErrorMessage(() => new Instance(m3), TypeError, /second argument must be an object/);
assertErrorMessage(() => new Instance(m3, {foo:null}), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m3, {foo:{bar:{}}}), TypeError, /import object field is not a Memory/);
assertErrorMessage(() => new Instance(m3, {foo:{bar:mem1Page}, baz:null}), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m3, {foo:{bar:mem1Page}, baz:{quux:mem1Page}}), TypeError, /import object field is not a Function/);
assertErrorMessage(() => new Instance(m3, {foo:null}), TypeError, /import object field 'foo' is not an Object/);
assertErrorMessage(() => new Instance(m3, {foo:{bar:{}}}), TypeError, /import object field 'bar' is not a Memory/);
assertErrorMessage(() => new Instance(m3, {foo:{bar:mem1Page}, baz:null}), TypeError, /import object field 'baz' is not an Object/);
assertErrorMessage(() => new Instance(m3, {foo:{bar:mem1Page}, baz:{quux:mem1Page}}), TypeError, /import object field 'quux' is not a Function/);
assertErrorMessage(() => new Instance(m3, {foo:{bar:mem1Page}, baz:{quux:()=>{}}}), TypeError, /imported Memory with incompatible maximum size/);
assertEq(new Instance(m3, {foo:{bar:mem1PageMax1}, baz:{quux:()=>{}}}) instanceof Instance, true);

const m4 = new Module(wasmTextToBinary('(module (import "baz" "quux") (import "foo" "bar" (memory 1 1)))'));
assertErrorMessage(() => new Instance(m4), TypeError, /second argument must be an object/);
assertErrorMessage(() => new Instance(m4, {baz:null}), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m4, {baz:{quux:{}}}), TypeError, /import object field is not a Function/);
assertErrorMessage(() => new Instance(m4, {baz:{quux:()=>{}}, foo:null}), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m4, {baz:{quux:()=>{}}, foo:{bar:()=>{}}}), TypeError, /import object field is not a Memory/);
assertErrorMessage(() => new Instance(m4, {baz:null}), TypeError, /import object field 'baz' is not an Object/);
assertErrorMessage(() => new Instance(m4, {baz:{quux:{}}}), TypeError, /import object field 'quux' is not a Function/);
assertErrorMessage(() => new Instance(m4, {baz:{quux:()=>{}}, foo:null}), TypeError, /import object field 'foo' is not an Object/);
assertErrorMessage(() => new Instance(m4, {baz:{quux:()=>{}}, foo:{bar:()=>{}}}), TypeError, /import object field 'bar' is not a Memory/);
assertErrorMessage(() => new Instance(m4, {baz:{quux:()=>{}}, foo:{bar:mem1Page}}), TypeError, /imported Memory with incompatible maximum size/);
assertEq(new Instance(m3, {baz:{quux:()=>{}}, foo:{bar:mem1PageMax1}}) instanceof Instance, true);

Expand Down Expand Up @@ -104,7 +104,7 @@ var importObj = {
get foo() { arr.push("foo") },
get baz() { arr.push("bad") },
};
assertErrorMessage(() => new Instance(m1, importObj), TypeError, /import object field is not an Object/);
assertErrorMessage(() => new Instance(m1, importObj), TypeError, /import object field 'foo' is not an Object/);
assertEq(arr.join(), "foo");

var arr = [];
Expand All @@ -115,7 +115,7 @@ var importObj = {
},
get baz() { arr.push("bad") },
};
assertErrorMessage(() => new Instance(m1, importObj), TypeError, /import object field is not a Function/);
assertErrorMessage(() => new Instance(m1, importObj), TypeError, /import object field 'bar' is not a Function/);
assertEq(arr.join(), "foo,bar");

var arr = [];
Expand Down
2 changes: 1 addition & 1 deletion js/src/js.msg
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ MSG_DEF(JSMSG_WASM_BAD_IMP_SIZE, 1, JSEXN_TYPEERR, "imported {0} with
MSG_DEF(JSMSG_WASM_BAD_IMP_MAX, 1, JSEXN_TYPEERR, "imported {0} with incompatible maximum size")
MSG_DEF(JSMSG_WASM_BAD_ELEMENT, 0, JSEXN_TYPEERR, "\"element\" property of table descriptor must be \"anyfunc\"")
MSG_DEF(JSMSG_WASM_BAD_IMPORT_ARG, 0, JSEXN_TYPEERR, "second argument must be an object")
MSG_DEF(JSMSG_WASM_BAD_IMPORT_FIELD, 1, JSEXN_TYPEERR, "import object field is not {0}")
MSG_DEF(JSMSG_WASM_BAD_IMPORT_FIELD, 2, JSEXN_TYPEERR, "import object field '{0}' is not {1}")
MSG_DEF(JSMSG_WASM_BAD_IMPORT_SIG, 0, JSEXN_TYPEERR, "imported function signature mismatch")
MSG_DEF(JSMSG_WASM_BAD_TABLE_VALUE, 0, JSEXN_TYPEERR, "can only assign WebAssembly exported functions to Table")
MSG_DEF(JSMSG_WASM_BAD_I64, 0, JSEXN_TYPEERR, "cannot pass i64 to or from JS")
Expand Down

0 comments on commit 5c449cb

Please sign in to comment.