Skip to content

Commit

Permalink
Correctly offset the arguments to calls (to make room for this alwa…
Browse files Browse the repository at this point in the history
…ys being the first argument).
  • Loading branch information
skinny85 committed Oct 7, 2023
1 parent 8b8df55 commit da2ba6e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.endoflineblog.truffle.part_13;

import org.openjdk.jmh.annotations.Benchmark;

/**
* A simple benchmark for calling an instance method of a user-defined class.
*/
public class FibonacciBenchmark extends TruffleBenchmark {
private static final String FIBONACCI_JS_FUNCTION = "" +
"function fib(n) { " +
" if (n > -2) { " +
" return Math.abs(n); " +
" } " +
" return fib(n + 1) + fib(n + 2); " +
"}";
private static final String FIBONACCI_JS_PROGRAM = FIBONACCI_JS_FUNCTION + "fib(-20);";

@Benchmark
public int recursive_ezs_eval() {
return this.truffleContext.eval("ezs", FIBONACCI_JS_PROGRAM).asInt();
}

@Benchmark
public int recursive_js_eval() {
return this.truffleContext.eval("js", FIBONACCI_JS_PROGRAM).asInt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,22 @@ protected Object getScope(EasyScriptLanguageContext context) {

private StringPrototype createStringPrototype() {
return new StringPrototype(
this.createCallTarget(CharAtMethodBodyExprNodeFactory.getInstance()));
this.createCallTarget(CharAtMethodBodyExprNodeFactory.getInstance(),
// built-in method implementation Nodes already have an argument for `this`,
// so there's no need to offset the method arguments
/* offsetArguments */ false));
}

private FunctionObject defineBuiltInFunction(NodeFactory<? extends BuiltInFunctionBodyExprNode> nodeFactory) {
return new FunctionObject(this.createCallTarget(nodeFactory),
return new FunctionObject(this.createCallTarget(nodeFactory, /* offsetArguments */ true),
nodeFactory.getExecutionSignature().size());
}

private CallTarget createCallTarget(NodeFactory<? extends BuiltInFunctionBodyExprNode> nodeFactory) {
private CallTarget createCallTarget(NodeFactory<? extends BuiltInFunctionBodyExprNode> nodeFactory,
boolean offsetArguments) {
int argumentCount = nodeFactory.getExecutionSignature().size();
ReadFunctionArgExprNode[] functionArguments = IntStream.range(0, argumentCount)
.mapToObj(i -> new ReadFunctionArgExprNode(i))
.mapToObj(i -> new ReadFunctionArgExprNode(offsetArguments ? i + 1 : i))
.toArray(ReadFunctionArgExprNode[]::new);
var rootNode = new BuiltInFuncRootNode(this,
nodeFactory.createNode((Object) functionArguments));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,25 @@ protected static Object targetIsNotAFunction(
}

private static Object[] extendArguments(Object[] arguments, FunctionObject function) {
// if the function object doesn't have a target
// (meaning, it's a function, not method, call),
// and we were called with at least as many arguments as the function takes,
// there's nothing to do
if (arguments.length >= function.argumentCount && function.methodTarget == null) {
return arguments;
}

// otherwise, create a new arguments array,
// saving the method target as the first element if it's non-null,
// and filling out the remaining arguments with 'undefined's
// if we got called with less of them than the function expects
Object[] ret = new Object[function.argumentCount];
for (int i = 0; i < function.argumentCount; i++) {
int j;
if (function.methodTarget == null) {
j = i;
// create a new array of arguments, reserving the first one for 'this' in functions
// (methods already have 'this' included) -
// which means we need to offset the remaining arguments by one
int extendedArgumentsLength = function.argumentCount +
(function.methodTarget == null ? 1 : 0);
Object[] ret = new Object[extendedArgumentsLength];
for (int i = 0; i < extendedArgumentsLength; i++) {
if (i == 0) {
// for 'this', if we don't have a method target, we need to use 'undefined'
ret[i] = function.methodTarget == null ? Undefined.INSTANCE : function.methodTarget;
} else {
if (i == 0) {
ret[0] = function.methodTarget;
continue;
} else {
j = i - 1;
}
// we need to offset the arguments by one
int j = i - 1;
ret[i] = j < arguments.length
? arguments[j]
// if a function was called with fewer arguments than it declares,
// we fill them with `undefined`
: Undefined.INSTANCE;
}
ret[i] = j < arguments.length ? arguments[j] : Undefined.INSTANCE;
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
public class ThisExprNode extends EasyScriptExprNode {
@Override
public Object executeGeneric(VirtualFrame frame) {
return frame.getArguments().length > 0
// if 'this' is available, it's passed as the first argument
? frame.getArguments()[0]
// if 'this' is not available, it's 'undefined'
: Undefined.INSTANCE;
// because of how we handle calls in FunctionDispatchNode,
// the `this` object is always in the first argument
return frame.getArguments()[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ private FuncDeclStmtNode parseSubroutineDecl(EasyScriptParser.Subroutine_declCon
int argumentCount = funcArgs.size();
// first, initialize the locals with function arguments
for (int i = 0; i < argumentCount; i++) {
localVariables.put(funcArgs.get(i).getText(), new FunctionArgument(i));
// offset the arguments by one,
// because the first argument is always `this`
localVariables.put(funcArgs.get(i).getText(), new FunctionArgument(i + 1));
}
this.localScopes.push(localVariables);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ void tearDown() {
@Test
void this_in_global_function_is_undefined() {
Value result = this.context.eval("ezs", "" +
"function returnThis() { " +
"function returnThis(ignored) { " +
" return this; " +
"} " +
"returnThis();"
"returnThis(3);"
);
assertTrue(result.isNull());
assertEquals("undefined", result.toString());
Expand Down

0 comments on commit da2ba6e

Please sign in to comment.