Skip to content

Commit

Permalink
Add option to skip the 'desugaring frame accesses' set in bcompile
Browse files Browse the repository at this point in the history
Looking up the prototype chain is probably faster natively in the runtime
than it is in interpreted bytecode.
  • Loading branch information
cscott committed Feb 10, 2020
1 parent 9188ba7 commit 386c036
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions bcompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ define(["text!bcompile.js", "bytecode-table"], function make_bcompile(bcompile_s

var dispatch = {};
// compilation state
var mkstate = function() {
var mkstate = function(dont_desugar_frame_get) {
// The result of a compilation: a function list and a literal list.
// We also need to count lexical scope nesting depth during compilation
var state = {
functions: [],
literals: [],
// internal
scope: 0
scope: 0,
desugar_frame_get: !dont_desugar_frame_get
};
// literal symbol table. Does string intern'ing too. Very simple.
state.literal = function(val) {
Expand Down Expand Up @@ -243,9 +244,12 @@ define(["text!bcompile.js", "bytecode-table"], function make_bcompile(bcompile_s
// This loop is actually optional; the parent chain will do
// the lookup correctly even if you don't take care to look
// in the exact right frame (like we do here)
while ( i < depth ) {
state.emit("get_slot_direct", state.literal("__proto__"));
i += 1;
// (might be faster to let the object model do this traversal)
if ( state.desugar_frame_get ) {
while ( i < depth ) {
state.emit("get_slot_direct", state.literal("__proto__"));
i += 1;
}
}
state.emit("get_slot_direct", state.literal(this.value));
};
Expand Down Expand Up @@ -350,6 +354,9 @@ define(["text!bcompile.js", "bytecode-table"], function make_bcompile(bcompile_s
if (this.first.arity === "name") {
var i = 0, depth = state.scope - this.first.scope.level;
state.emit("push_frame");
// Unlike the look in dispatch.name, if we left off the
// parent chain traversal here we wouldn't be able to affect
// variables in the outer scope. So this isn't optional.
while ( i < depth ) {
state.emit("get_slot_direct", state.literal("__proto__"));
i += 1;
Expand Down Expand Up @@ -686,8 +693,8 @@ define(["text!bcompile.js", "bytecode-table"], function make_bcompile(bcompile_s
return;
};

var bcompile = function (parse_tree) {
var state = mkstate();
var bcompile = function (parse_tree, dont_desugar_frame_get) {
var state = mkstate(dont_desugar_frame_get);
state.current_func = state.new_function(0);
state.bcompile_stmts(parse_tree);
if (state.current_func.can_fall_off) {
Expand Down
2 changes: 1 addition & 1 deletion write-lua-bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ define(['./parse', './bcompile', './bytecode-table', './top-level', './str-escap
var compile_from_source = function (source, as_object) {
source = source || '{ return 1+2; }';
var tree = parse(source, TOP_LEVEL);
var bc = bcompile(tree);
var bc = bcompile(tree, true/*don't desugar frame get*/);
var result = as_object ? bc : bc.encode();
return result;
};
Expand Down

0 comments on commit 386c036

Please sign in to comment.