Skip to content

Commit

Permalink
[js] Make nqp::can and nqp::findmethod make use of HOW.find_method.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Nov 18, 2016
1 parent 4693bb8 commit 3f0dece
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/vm/js/Operations.nqp
Expand Up @@ -824,8 +824,8 @@ class QAST::OperationsJS {

add_simple_op('backtracestrings', $T_OBJ, [$T_OBJ]);

add_simple_op('findmethod', $T_OBJ, [$T_OBJ, $T_STR], :side_effects, :decont(0));
add_simple_op('can', $T_INT, [$T_OBJ, $T_STR], :side_effects, :decont(0));
add_simple_op('findmethod', $T_OBJ, [$T_OBJ, $T_STR], :side_effects, :decont(0), :ctx);
add_simple_op('can', $T_INT, [$T_OBJ, $T_STR], :side_effects, :decont(0), :ctx);

add_simple_op('istype', $T_INT, [$T_OBJ, $T_OBJ], :side_effects, :ctx, :decont(0, 1));

Expand Down
36 changes: 25 additions & 11 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -204,9 +204,6 @@ op.getcodeobj = function(codeRef) {
return codeRef.codeObj;
};

op.findmethod = function(obj, method) {
return obj._STable.methodCache[method];
};

op.istype = function(ctx, obj, type) {
/* Null always type checks false. */
Expand Down Expand Up @@ -278,8 +275,7 @@ op.setmethcache = function(obj, cache) {
};

op.setmethcacheauth = function(obj, isAuth) {
/* TODO we currently assume method caches are always authorative
sadly that's not always the case*/
obj._STable.methodCacheAuth = isAuth;
return obj;
};

Expand Down Expand Up @@ -309,14 +305,32 @@ op.newtype = function(how, repr) {
return REPR.typeObjectFor(how);
};

/* HACK - we need to handle can properly */
op.can = function(obj, method) {
function find_method(ctx, obj, name) {
if (obj._STable.methodCache) {
var hasMethod = obj._STable.methodCache.hasOwnProperty(name);
if (hasMethod) {
return obj._STable.methodCache[name];
}
if (obj._STable.methodCacheAuth) {
return Null;
}
}
return obj._STable.HOW.find_method(ctx, null, obj._STable.HOW, obj, name);
}

op.can = function(ctx, obj, name) {
/* HACK those things should have an STable rather then be special cased */
if (typeof obj !== 'object' || obj instanceof NQPInt || obj instanceof CodeRef || obj instanceof Hash || obj instanceof NQPArray) return 0;
if (!obj._STable.methodCache) {
console.warn('we have no method cache, checking: ' + method);
return 0;

return find_method(ctx, obj, name) === Null ? 0 : 1;
};

op.findmethod = function(ctx, obj, name) {
var method = find_method(ctx, obj, name);
if (method === Null) {
throw new NQPException("Cannot find method '" + name + "' on object of type " + obj._STable.debugName);
}
return obj._STable.methodCache.hasOwnProperty(method) ? 1 : 0;
return method;
};

op.getcodename = function(code) {
Expand Down

0 comments on commit 3f0dece

Please sign in to comment.