Bug 868658 - OdinMonkey: also require explicit coercion of function-pointer calls (r=sstangl)

This commit is contained in:
Luke Wagner 2013-05-03 18:38:00 -07:00
parent 4c894f416f
commit e90f668307
2 changed files with 10 additions and 7 deletions

View File

@ -3303,14 +3303,14 @@ CheckInternalCall(FunctionCompiler &f, ParseNode *callNode, const ModuleCompiler
// against the declared return type when the definition was encountered.
if (!(callee.returnType() <= use))
return f.fail("return type of callee not compatible with use", callNode);
return f.fail("return type of callee incompatible with use", callNode);
*type = use.toReturnType();
return true;
}
static bool
CheckFuncPtrCall(FunctionCompiler &f, ParseNode *callNode, MDefinition **def, Type *type)
CheckFuncPtrCall(FunctionCompiler &f, ParseNode *callNode, Use use, MDefinition **def, Type *type)
{
ParseNode *callee = CallCallee(callNode);
ParseNode *elemBase = ElemBase(callee);
@ -3357,7 +3357,10 @@ CheckFuncPtrCall(FunctionCompiler &f, ParseNode *callNode, MDefinition **def, Ty
if (!f.funcPtrCall(*table, indexDef, args, def))
return false;
*type = table->sig().returnType().toType();
if (!(table->sig().returnType() <= use))
return f.fail("return type of callee incompatible with use", callNode);
*type = use.toReturnType();
return true;
}
@ -3451,7 +3454,7 @@ CheckCall(FunctionCompiler &f, ParseNode *call, Use use, MDefinition **def, Type
ParseNode *callee = CallCallee(call);
if (callee->isKind(PNK_ELEM))
return CheckFuncPtrCall(f, call, def, type);
return CheckFuncPtrCall(f, call, use, def, type);
if (!callee->isKind(PNK_NAME))
return f.fail("Unexpected callee expression type", callee);

View File

@ -34,16 +34,16 @@ assertEq(asmLink(asmCompile(USE_ASM + "function f() {return 42} function g() {re
assertEq(asmLink(asmCompile(USE_ASM + "var i=0,j=0; function f() {return i|0} function g() {return j|0} function h(x) { x=x|0; i=5;j=10; return tbl2[x&3]()|0 } var tbl1=[f,g]; var tbl2=[g,g,g,f]; return h"))(3), 5);
assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + "var ffi=imp.ffi; function f() {return ffi()|0} function g() {return 13} function h(x) { x=x|0; return tbl2[x&3]()|0 } var tbl2=[g,g,g,f]; return h"), null, {ffi:function(){return 20}})(3), 20);
assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + "var ffi=imp.ffi; var i=0; function f() {return (~~ffi()+i)|0} function g() {return 13} function h(x) { x=x|0; i=2; return tbl2[x&3]()|0 } var tbl2=[g,g,g,f]; return h"), null, {ffi:function(){return 20}})(3), 22);
assertEq(asmLink(asmCompile(USE_ASM + "function f(i) {i=i|0; return +((i+1)|0)} function g(d) { d=+d; return +(d+2.5) } function h(i,j) { i=i|0;j=j|0; return +tbl2[i&1](tbl1[i&1](j)) } var tbl1=[f,f]; var tbl2=[g,g]; return h"))(0,10), 11+2.5);
assertEq(asmLink(asmCompile(USE_ASM + "function f(i) {i=i|0; return +((i+1)|0)} function g(d) { d=+d; return +(d+2.5) } function h(i,j) { i=i|0;j=j|0; return +tbl2[i&1](+tbl1[i&1](j)) } var tbl1=[f,f]; var tbl2=[g,g]; return h"))(0,10), 11+2.5);
assertAsmTypeFail(USE_ASM + "function f() {return 42} function g() { return tbl[0]()|0 } var tbl=[f]; return g");
assertEq(asmLink(asmCompile(USE_ASM + "function f() {return 42} function g() { return tbl[0&0]()|0 } var tbl=[f]; return g"))(), 42);
assertEq(asmLink(asmCompile(USE_ASM + "function f1() {return 42} function f2() {return 13} function g() { return tbl[1&1]()|0 } var tbl=[f1,f2]; return g"))(), 13);
var f = asmLink(asmCompile(USE_ASM + "function f1(d) {d=+d; return +(d/2.0)} function f2(d) {d=+d; return +(d+10.0)} function g(i,j) { i=i|0;j=+j; return +tbl[i&1](tbl[(i+1)&1](j)) } var tbl=[f1,f2]; return g"));
var f = asmLink(asmCompile(USE_ASM + "function f1(d) {d=+d; return +(d/2.0)} function f2(d) {d=+d; return +(d+10.0)} function g(i,j) { i=i|0;j=+j; return +tbl[i&1](+tbl[(i+1)&1](j)) } var tbl=[f1,f2]; return g"));
assertEq(f(0,10.2), (10.2+10)/2);
assertEq(f(1,10.2), (10.2/2)+10);
var f = asmLink(asmCompile('glob','imp', USE_ASM + "var ffi=imp.ffi; function f(){return 13} function g(){return 42} function h(i) { i=i|0; var j=0; ffi(1); j=TBL[i&7](); ffi(1.5); return j|0 } var TBL=[f,g,f,f,f,f,f,f]; return h"), null, {ffi:function(){}});
var f = asmLink(asmCompile('glob','imp', USE_ASM + "var ffi=imp.ffi; function f(){return 13} function g(){return 42} function h(i) { i=i|0; var j=0; ffi(1); j=TBL[i&7]()|0; ffi(1.5); return j|0 } var TBL=[f,g,f,f,f,f,f,f]; return h"), null, {ffi:function(){}});
for (var i = 0; i < 100; i++)
assertEq(f(i), (i%8 == 1) ? 42 : 13);