Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[js] Add very basic start of an implementation of the nativecall ops.
Pass t/nativecall/01-basic.t
  • Loading branch information
pmurias committed Jan 2, 2016
1 parent d86a422 commit ed8ed36
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -9,6 +9,14 @@ my $T_BOOL := 4; # Something that can be used in boolean context in javascript.
my $T_VOID := -1; # Value of this type shouldn't exist, we use a "" as the expr
my $T_NONVAL := -2; # something that is not a nqp value

my sub join($delim, $array) {
my $ret := '';
for $array -> $part {
$ret := $ret ~ $part;
}
$ret;
}

# turn a string into a javascript literal
sub quote_string($str) {
my $out := '';
Expand Down Expand Up @@ -1526,6 +1534,11 @@ class QAST::OperationsJS {
add_simple_op('curcode', $T_OBJ, []);
add_simple_op('callercode', $T_OBJ, []);

# Native Call
add_simple_op('buildnativecall', $T_INT, [$T_OBJ, $T_STR, $T_STR, $T_STR, $T_OBJ, $T_OBJ], :sideffects, :ctx);
add_simple_op('nativecall', $T_OBJ, [$T_OBJ, $T_OBJ, $T_OBJ], :sideffects);

# Continuations
add_simple_op('continuationreset', $T_OBJ, [$T_OBJ, $T_OBJ], :sideffects, :ctx);
add_simple_op('continuationinvoke', $T_OBJ, [$T_OBJ, $T_OBJ], :sideffects, :ctx, :cps_aware);

Expand Down
4 changes: 3 additions & 1 deletion src/vm/js/bin/run_tests.pl
Expand Up @@ -20,6 +20,8 @@
my @runtime_unit_tests = qw(t/js/varint.js);
my @continuation_tests = qw(t/js/continuations.t);

my @native_call_tests = qw(t/nativecall/01-basic.t);

my $node_version = `node -v`;

# avoid failures on the old node.js travis version
Expand All @@ -28,4 +30,4 @@
@nqp_tests = grep {!/19|78/} @nqp_tests;
}

$harness->runtests(@runtime_unit_tests, @nqp_tests, @moar_tests, @regex, @serialization, @qast, @js_specific, @continuation_tests);
$harness->runtests(@runtime_unit_tests, @nqp_tests, @moar_tests, @regex, @serialization, @qast, @js_specific, @continuation_tests, @native_call_tests);
32 changes: 32 additions & 0 deletions src/vm/js/nqp-runtime/nativecall.js
@@ -0,0 +1,32 @@
var op = {};
var ffi = require('ffi');
var ref = require('ref');
exports.op = op;

function mapType(typeHash) {
var nativeCallType = typeHash.content.type;
if (nativeCallType === 'void') {
return ref.types.void;
} else if (nativeCallType === 'cpointer') {
return ref.refType(ref.types.void);
} else if (nativeCallType === 'utf8str') {
return ref.types.CString;
} else {
throw "type not supported: " + nativeCallType;
}
}

op.buildnativecall = function(ctx, target, libname, symbol, convention, args, returns) {
try {
var symbols = {};
symbols[symbol] = [mapType(returns), args.map(mapType)];
target.lib = ffi.Library(libname === '' ? null : libname, symbols);
target.symbol = symbol;
} catch (e) {
ctx.die(e.message);
}
};

op.nativecall = function(returns, callObject, args) {
return callObject.lib[callObject.symbol].apply(callObject.lib, args)
};
4 changes: 3 additions & 1 deletion src/vm/js/nqp-runtime/package.json
Expand Up @@ -21,6 +21,8 @@
"nqp-js-io": "pmurias/nqp-js-io",
"sha1": "*",
"sleep": "*",
"xorshift": "*"
"xorshift": "*",
"ffi": "*",
"ref": "*"
}
}
9 changes: 9 additions & 0 deletions src/vm/js/nqp-runtime/reprs.js
Expand Up @@ -717,6 +717,15 @@ exports.P6bigint = P6bigint;

/* Stubs */

function NativeCall() {}
NativeCall.prototype.create_obj_constructor = basic_constructor;
NativeCall.prototype.allocate = basic_allocate;
exports.NativeCall = NativeCall;

function CPointer() {}
CPointer.prototype.create_obj_constructor = basic_constructor;
exports.CPointer = CPointer;

function ReentrantMutex() {}
ReentrantMutex.prototype.create_obj_constructor = basic_constructor;

Expand Down
3 changes: 3 additions & 0 deletions src/vm/js/nqp-runtime/runtime.js
Expand Up @@ -36,6 +36,9 @@ load_ops(deserialization);
var serialization = require('./serialization.js');
load_ops(serialization);

var nativecall = require('./nativecall.js');
load_ops(nativecall);

exports.CodeRef = require('./code-ref.js');

exports.CurLexpad = require('./curlexpad.js');
Expand Down

0 comments on commit ed8ed36

Please sign in to comment.