Skip to content

Commit

Permalink
[js] Pass the HLL object around instead of just the name
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Aug 31, 2017
1 parent e1d0c92 commit 7729dad
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 77 deletions.
16 changes: 8 additions & 8 deletions src/vm/js/Compiler.nqp
Expand Up @@ -412,7 +412,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
}
else {
$pos_slurpy := 1;
set_variable($param, "nqp.slurpyArray({quote_string($*HLL)}, Array.prototype.slice.call(arguments,{+@sig}))");
set_variable($param, "nqp.slurpyArray(HLL, Array.prototype.slice.call(arguments,{+@sig}))");
}
} else {
my int $type := self.type_from_typeobj($param.returns);
Expand Down Expand Up @@ -575,7 +575,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
%convert{$T_INT} := 'intToObj';
%convert{$T_NUM} := 'numToObj';
%convert{$T_STR} := 'strToObj';
return Chunk.new($T_OBJ, "nqp.{%convert{$got}}({quote_string($*HLL)}, {$chunk.expr})", $chunk);
return Chunk.new($T_OBJ, "nqp.{%convert{$got}}(HLL, {$chunk.expr})", $chunk);
}
}

Expand Down Expand Up @@ -1057,7 +1057,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
if $node.has_exit_handler {
$result_for_exit_handler := $*BLOCK.add_tmp;
$wrap_in_try := 1;
$exit_handler := "\} finally \{nqp.exitHandler($*CTX, {quote_string($*HLL)}, $result_for_exit_handler);\n";
$exit_handler := "\} finally \{nqp.exitHandler($*CTX, HLL, $result_for_exit_handler);\n";
}

if $*BLOCK.pass_on_exceptions {
Expand Down Expand Up @@ -1331,7 +1331,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
"var sh = nqp.createArray([{nqp::join(',',@sh)}]);\n"
~ "var sc = nqp.op.createsc({quote_string(nqp::scgethandle($sc))});\n"
~ self.emit_code_refs_list($ast)
, "nqp.op.deserialize({quote_string($*HLL)}, $quoted_data,sc,sh,code_refs,null,cuids,function() \{{self.setup_wvals}\});\n"
, "nqp.op.deserialize(HLL, $quoted_data,sc,sh,code_refs,null,cuids,function() \{{self.setup_wvals}\});\n"
~ "nqp.op.scsetdesc(sc,{quote_string(nqp::scgetdesc($sc))});\n");
}

Expand Down Expand Up @@ -1487,7 +1487,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {

my int $comp_mode := $node.compilation_mode;

my str $set_hll := $*HLL ?? "nqp.setCodeRefHLL(cuids, {quote_string($*HLL)});\n" !! '';
my str $set_hll := "nqp.setCodeRefHLL(cuids, HLL);\n";

my $set_code_objects := self.set_code_objects;
my @setup := [$pre , $comp_mode ?? self.create_sc($node) !! '', $set_code_objects, self.declare_js_vars($*BLOCK.tmps), self.declare_js_vars($*BLOCK.js_lexicals), self.capture_inners($*BLOCK), self.clone_inners($*BLOCK), $set_hll, $post, $body];
Expand All @@ -1510,7 +1510,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {

multi method as_js(QAST::ParamTypeCheck $node) {
my $check := self.as_js($node[0], :want($T_BOOL));
Chunk.void($check, "if (!{$check.expr}) return nqp.paramcheckfailed({quote_string($*HLL)}, $*CTX, Array.prototype.slice.call(arguments));\n");
Chunk.void($check, "if (!{$check.expr}) return nqp.paramcheckfailed(HLL, $*CTX, Array.prototype.slice.call(arguments));\n");
}

my %default_value := nqp::hash($T_OBJ, 'nqp.Null', $T_INT, '0', $T_NUM, '0', $T_STR, 'nqp.null_s');
Expand Down Expand Up @@ -1807,7 +1807,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
my $suffix := self.suffix_from_type($type);
my $get := self.get_var($var);
my $set := self.set_var($var, 'value');
Chunk.new($T_OBJ, "nqp.lexRef{$suffix}({quote_string($*HLL)}, function() \{return $get\}, function(value) \{$set\})", :node($var));
Chunk.new($T_OBJ, "nqp.lexRef{$suffix}(HLL, function() \{return $get\}, function(value) \{$set\})", :node($var));
}
}
}
Expand Down Expand Up @@ -1881,7 +1881,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
}


Chunk.new($T_OBJ, "nqp.attrRef{$suffix}({quote_string($*HLL)}, function() \{return $get\}, function(value) \{$set\})", :node($var));
Chunk.new($T_OBJ, "nqp.attrRef{$suffix}(HLL, function() \{return $get\}, function(value) \{$set\})", :node($var));
}
elsif $var.scope eq 'attribute' {
my @types := [$T_OBJ, $T_INT, $T_NUM, $T_STR];
Expand Down
6 changes: 3 additions & 3 deletions src/vm/js/Operations.nqp
Expand Up @@ -38,9 +38,9 @@ class QAST::OperationsJS {

if $hll {
if $method_call {
nqp::splice(@exprs, nqp::list(quote_string($*HLL)), 1, 0);
nqp::splice(@exprs, 'HLL', 1, 0);
} else {
@exprs.unshift(quote_string($*HLL));
@exprs.unshift('HLL');
}
}

Expand Down Expand Up @@ -577,7 +577,7 @@ class QAST::OperationsJS {
@exprs.push($chunk.expr);
}

my $hll := $type == $T_OBJ ?? quote_string($*HLL) ~ ',' !! '';
my $hll := $type == $T_OBJ ?? 'HLL,' !! '';
@setup.push($list ~ " = nqp.list" ~ $suffix ~ "(" ~ $hll ~ "[" ~ nqp::join(',', @exprs) ~ "]);\n");

Chunk.new($T_OBJ, $list , @setup, :$node);
Expand Down
8 changes: 4 additions & 4 deletions src/vm/js/nqp-runtime/bignum.js
Expand Up @@ -244,15 +244,15 @@ op.bool_I = function(n) {
return intishBool(getBI(n).toNumber());
};

op.radix_I = function(hllName, radix, str, zpos, flags, type) {
op.radix_I = function(currentHLL, radix, str, zpos, flags, type) {
var extracted = core.radixHelper(radix, str, zpos, flags);
if (extracted == null) {
return hll.slurpyArray(hllName, [makeBI(type, bignum(0)), makeBI(type, bignum(1)), -1]);
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(0)), makeBI(type, bignum(1)), -1]);
}

if (radix == 10 || radix == 16) {
var pow = bignum(radix).pow(extracted.power);
return hll.slurpyArray(hllName, [makeBI(type, bignum(extracted.number, radix)), makeBI(type, pow), extracted.offset]);
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(extracted.number, radix)), makeBI(type, pow), extracted.offset]);
} else {
var n = extracted.number;
var base = bignum(1);
Expand All @@ -271,6 +271,6 @@ op.radix_I = function(hllName, radix, str, zpos, flags, type) {

if (n[0] == '-') result = result.neg();

return hll.slurpyArray(hllName, [makeBI(type, result), makeBI(type, base), extracted.offset]);
return hll.slurpyArray(currentHLL, [makeBI(type, result), makeBI(type, base), extracted.offset]);
}
};
20 changes: 10 additions & 10 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -79,13 +79,13 @@ function radixHelper(radix, str, zpos, flags) {

exports.radixHelper = radixHelper;

op.radix = function(hllName, radix, str, zpos, flags) {
op.radix = function(currentHLL, radix, str, zpos, flags) {
var extracted = radixHelper(radix, str, zpos, flags);
if (extracted == null) {
return hll.slurpyArray(hllName, [0, 1, -1]);
return hll.slurpyArray(currentHLL, [0, 1, -1]);
}
var pow = Math.pow(radix, extracted.power);
return hll.slurpyArray(hllName, [parseInt(extracted.number, radix), pow, extracted.offset]);
return hll.slurpyArray(currentHLL, [parseInt(extracted.number, radix), pow, extracted.offset]);
};

op.setdebugtypename = function(type, debugName) {
Expand Down Expand Up @@ -1058,11 +1058,11 @@ op.getstaticcode = function(codeRef) {
return codeRef.staticCode;
};

op.backtracestrings = function(hllName, exception) {
return hll.list(hllName, [exception.stack.replace(/^Error: .*\n/, '')]);
op.backtracestrings = function(currentHLL, exception) {
return hll.list(currentHLL, [exception.stack.replace(/^Error: .*\n/, '')]);
};

op.backtrace = function(hllName, exception) {
op.backtrace = function(currentHLL, exception) {
if (exception.$$ctx) {
let ctx = exception.$$ctx.$$skipHandlers();

Expand Down Expand Up @@ -1114,9 +1114,9 @@ op.backtrace = function(hllName, exception) {

ctx = ctx.$$caller;
}
return hll.list(hllName, rows);
return hll.list(currentHLL, rows);
} else {
return hll.list(hllName, []);
return hll.list(currentHLL, []);
}
};

Expand Down Expand Up @@ -1272,8 +1272,8 @@ op.islist = function(list) {
return (list._STable && list._STable.REPR instanceof reprs.VMArray) ? 1 : 0;
};

op.split = function(hllName, separator, string) {
return hll.slurpyArray(hllName, string !== '' ? string.split(separator) : []);
op.split = function(currentHLL, separator, string) {
return hll.slurpyArray(currentHLL, string !== '' ? string.split(separator) : []);
};

op.exception = function() {
Expand Down
4 changes: 2 additions & 2 deletions src/vm/js/nqp-runtime/deserialization.js
Expand Up @@ -41,7 +41,7 @@ module.exports.wval = function(handle, idx) {
return serializationContexts[handle].rootObjects[idx];
};

op.deserialize = function(hllName, blob, sc, sh, codeRefs, conflict, cuids, setupWVals) {
op.deserialize = function(currentHLL, blob, sc, sh, codeRefs, conflict, cuids, setupWVals) {
var buffer = new Buffer(blob, 'base64');
sc.codeRefs = codeRefs.array;

Expand All @@ -54,7 +54,7 @@ op.deserialize = function(hllName, blob, sc, sh, codeRefs, conflict, cuids, setu
sh = sh.array;
var cursor = new BinaryCursor(buffer, 0, sh, sc);

cursor.deserialize(sc, cuids, setupWVals, hll.getHLL(hllName));
cursor.deserialize(sc, cuids, setupWVals, currentHLL);
};

op.createsc = function(handle) {
Expand Down
14 changes: 6 additions & 8 deletions src/vm/js/nqp-runtime/hll.js
Expand Up @@ -85,6 +85,8 @@ exports.hllConfigs = hllConfigs;
function getHLL(language) {
if (!hllConfigs[language]) {
hllConfigs[language] = new Map;
hllConfigs[language].set('slurpy_array', BOOT.Array)
hllConfigs[language].set('list', BOOT.Array)

// For serialization purposes
hllConfigs[language].set('name', language);
Expand Down Expand Up @@ -115,16 +117,12 @@ op.settypehllrole = function(type, role) {
return type;
};

exports.slurpyArray = function(hllName, array) {
var slurpyArray;
if (hllConfigs[hllName]) slurpyArray = hllConfigs[hllName].get('slurpy_array');
if (slurpyArray === undefined) slurpyArray = BOOT.Array;
exports.slurpyArray = function(currentHLL, array) {
const slurpyArray = currentHLL.get('slurpy_array');
return slurpyArray._STable.REPR.allocateFromArray(slurpyArray._STable, array);
};

exports.list = function(hllName, array) {
var list;
if (hllConfigs[hllName]) list = hllConfigs[hllName].get('list');
if (list === undefined) list = BOOT.Array;
exports.list = function(currentHLL, array) {
const list = currentHLL.get('list');
return list._STable.REPR.allocateFromArray(list._STable, array);
};
48 changes: 24 additions & 24 deletions src/vm/js/nqp-runtime/refs.js
Expand Up @@ -3,8 +3,8 @@ let hll = require('./hll.js');
let helpers = exports.helpers = {};
let op = exports.op = {};

function attrRef_i(hllName, get, set) {
var refType = hll.hllConfigs[hllName].get('int_attr_ref');
function attrRef_i(currentHLL, get, set) {
var refType = currentHLL.get('int_attr_ref');
if (refType === undefined) {
throw 'No int attribute reference type registered for current HLL';
}
Expand All @@ -15,8 +15,8 @@ function attrRef_i(hllName, get, set) {
return ref;
}

function attrRef_n(hllName, get, set) {
var refType = hll.hllConfigs[hllName].get('num_attr_ref');
function attrRef_n(currentHLL, get, set) {
var refType = currentHLL.get('num_attr_ref');
if (refType === undefined) {
throw 'No num attribute reference type registered for current HLL';
}
Expand All @@ -27,8 +27,8 @@ function attrRef_n(hllName, get, set) {
return ref;
}

function attrRef_s(hllName, get, set) {
var refType = hll.hllConfigs[hllName].get('str_attr_ref');
function attrRef_s(currentHLL, get, set) {
var refType = currentHLL.get('str_attr_ref');
if (refType === undefined) {
throw 'No str attribute reference type registered for current HLL';
}
Expand All @@ -39,26 +39,26 @@ function attrRef_s(hllName, get, set) {
return ref;
}

op.getattrref_i = function(hllName, obj, classHandle, attrName) {
return attrRef_i(hllName,
op.getattrref_i = function(currentHLL, obj, classHandle, attrName) {
return attrRef_i(currentHLL,
() => obj.$$getattr_i(classHandle, attrName),
value => obj.$$bindattr_i(classHandle, attrName, value));
};

op.getattrref_n = function(hllName, obj, classHandle, attrName) {
return attrRef_n(hllName,
op.getattrref_n = function(currentHLL, obj, classHandle, attrName) {
return attrRef_n(currentHLL,
() => obj.$$getattr_n(classHandle, attrName),
value => obj.$$bindattr_n(classHandle, attrName, value));
};

op.getattrref_s = function(hllName, obj, classHandle, attrName) {
return attrRef_s(hllName,
op.getattrref_s = function(currentHLL, obj, classHandle, attrName) {
return attrRef_s(currentHLL,
() => obj.$$getattr_s(classHandle, attrName),
value => obj.$$bindattr_s(classHandle, attrName, value));
};

helpers.lexRef_i = function(hllName, get, set) {
var refType = hll.hllConfigs[hllName].get('int_lex_ref');
helpers.lexRef_i = function(currentHLL, get, set) {
var refType = currentHLL.get('int_lex_ref');
if (refType === undefined) {
throw 'No int lexical reference type registered for current HLL';
}
Expand All @@ -69,8 +69,8 @@ helpers.lexRef_i = function(hllName, get, set) {
return ref;
};

helpers.lexRef_s = function(hllName, get, set) {
var refType = hll.hllConfigs[hllName].get('str_lex_ref');
helpers.lexRef_s = function(currentHLL, get, set) {
var refType = currentHLL.get('str_lex_ref');
if (refType === undefined) {
throw 'No str lexical reference type registered for current HLL';
}
Expand All @@ -81,8 +81,8 @@ helpers.lexRef_s = function(hllName, get, set) {
return ref;
};

helpers.lexRef_n = function(hllName, get, set) {
var refType = hll.hllConfigs[hllName].get('num_lex_ref');
helpers.lexRef_n = function(currentHLL, get, set) {
var refType = currentHLL.get('num_lex_ref');
if (refType === undefined) {
throw 'No num lexical reference type registered for current HLL';
}
Expand All @@ -93,8 +93,8 @@ helpers.lexRef_n = function(hllName, get, set) {
return ref;
};

op.atposref_i = function(hllName, obj, index) {
var refType = hll.hllConfigs[hllName].get('int_pos_ref');
op.atposref_i = function(currentHLL, obj, index) {
var refType = currentHLL.get('int_pos_ref');
if (refType === undefined) {
throw 'No int lexical reference type registered for current HLL';
}
Expand All @@ -105,8 +105,8 @@ op.atposref_i = function(hllName, obj, index) {
return ref;
};

op.atposref_n = function(hllName, obj, index) {
var refType = hll.hllConfigs[hllName].get('num_pos_ref');
op.atposref_n = function(currentHLL, obj, index) {
var refType = currentHLL.get('num_pos_ref');
if (refType === undefined) {
throw 'No num lexical reference type registered for current HLL';
}
Expand All @@ -117,8 +117,8 @@ op.atposref_n = function(hllName, obj, index) {
return ref;
};

op.atposref_s = function(hllName, obj, index) {
var refType = hll.hllConfigs[hllName].get('str_pos_ref');
op.atposref_s = function(currentHLL, obj, index) {
var refType = currentHLL.get('str_pos_ref');
if (refType === undefined) {
throw 'No str lexical reference type registered for current HLL';
}
Expand Down

0 comments on commit 7729dad

Please sign in to comment.