Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement nqp::callercode.
  • Loading branch information
pmurias committed Feb 14, 2015
1 parent 2eb3dc1 commit 32535d5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -1055,7 +1055,9 @@ class QAST::OperationsJS {

add_simple_op('getcodeobj', $T_OBJ, [$T_OBJ]);
add_simple_op('setcodeobj', $T_OBJ, [$T_OBJ, $T_OBJ], :sideffects);

add_simple_op('curcode', $T_OBJ, []);
add_simple_op('callercode', $T_OBJ, []);

method compile_op($comp, $op, :$want) {
my str $name := $op.op;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/js/nqp-runtime/code-ref.js
Expand Up @@ -8,7 +8,7 @@ CodeRef.prototype.block = function(func) {
func.codeRef = this;
};

CodeRef.prototype.$apply = function(argsArray) {
CodeRef.prototype.$apply = function _(argsArray) {
return this.$call.apply(this, argsArray);
};

Expand Down
19 changes: 19 additions & 0 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -221,6 +221,25 @@ op.curcode = function() {
return current.codeRef;
};

op.callercode = function() {
var current = arguments.callee.caller;

/* Skip all fake first _ functions so we can skip a real one*/
while (current.name == '_') {
current = current.caller;
}

/* Skip a real function */
current = current.caller;

/* Skip all fake _ functions to get to a real one*/
while (current.name == '_') {
current = current.caller;
}

return current.codeRef;
};


// TODO benchmark and pick a fast way of doing this
op.splice = function(target, source, offset, length) {
Expand Down
6 changes: 3 additions & 3 deletions src/vm/js/nqp-runtime/sixmodel.js
Expand Up @@ -40,16 +40,16 @@ STable.prototype.setboolspec = function(mode, method) {

STable.prototype.setinvokespec = function(classHandle, attrName, invocationHandler) {
// TODO take classHandle into account
this.obj_constructor.prototype.$call = function() {
this.obj_constructor.prototype.$call = function _() {
return this[attrName].$call.apply(this, arguments);
};
this.obj_constructor.prototype.$apply = function(args) {
this.obj_constructor.prototype.$apply = function _(args) {
return this[attrName].$apply(args);
};
};

function injectMethod(proto, name, method) {
proto[name] = function() {
proto[name] = function _() {
// console.log("calling method:",name,method);
if (method.$call) {
var args = [];
Expand Down

0 comments on commit 32535d5

Please sign in to comment.