Browse files

[js] Move the cost of getting of the current code ref to when it's ac…

…tually done.
  • Loading branch information...
1 parent 2491176 commit 956c4ce6b454437abd0b9825d18ecc59ff523f89 @pmurias pmurias committed Jul 25, 2016
Showing with 18 additions and 14 deletions.
  1. +4 −4 src/vm/js/Compiler.nqp
  2. +3 −3 src/vm/js/Operations.nqp
  3. +8 −4 src/vm/js/nqp-runtime/runtime.js
  4. +3 −3 src/vm/js/nqp-runtime/serialization.js
View
8 src/vm/js/Compiler.nqp
@@ -972,7 +972,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
my $code_ref_attr := self.code_ref_attr($node.cuid);
- my $create_ctx := self.create_ctx($*CTX, :code_ref('this instanceof nqp.CodeRef ? this : this.' ~ $code_ref_attr));
+ my $create_ctx := self.create_ctx($*CTX, :code_ref('this'), :code_ref_attr(quote_string($code_ref_attr)));
if nqp::istype($stmts, ChunkCPS) {
}
else {
@@ -1003,7 +1003,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
else {
# We need to override when deserializing closures
# TODO - think if the override is the right things
- @function[3] := self.create_ctx($*CTX, :code_ref('$$codeRef'));
+ @function[3] := self.create_ctx($*CTX, :code_ref('$$codeRef'), :code_ref_attr('null'));
%!serialized_code_ref_info{$node.cuid} := SerializedCodeRefInfo.new(
closure_template => ChunkEscaped.new(@function),
ctx => $*CTX,
@@ -1123,9 +1123,9 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
$*BLOCK.outer ?? $*BLOCK.outer.ctx !! 'null';
}
- method create_ctx($name, :$code_ref) {
+ method create_ctx($name, :$code_ref, :$code_ref_attr) {
# TODO think about contexts
- "var $name = new nqp.Ctx(caller_ctx, {self.outer_ctx}, $code_ref);\n";
+ "var $name = new nqp.Ctx(caller_ctx, {self.outer_ctx}, $code_ref, $code_ref_attr);\n";
}
multi method as_js(QAST::IVal $node, :$want, :$cps) {
View
6 src/vm/js/Operations.nqp
@@ -740,7 +740,7 @@ class QAST::OperationsJS {
my $*CTX := $*HANDLER_CTX;
my $body := $comp.as_js($protected, :$want);
return Chunk.new($want, $try_ret, [
- "var $*CTX = new nqp.Ctx($outer_ctx, $outer_ctx, $outer_ctx.codeRef);\n",
+ "var $*CTX = new nqp.Ctx($outer_ctx, $outer_ctx, $outer_ctx.callThis, $outer_ctx.codeRefAttr);\n",
$handle,
"try \{",
$body,
@@ -1447,8 +1447,8 @@ class QAST::OperationsJS {
add_simple_op('radix_I', $T_OBJ, [$T_INT, $T_STR, $T_INT, $T_INT, $T_OBJ]);
- add_simple_op('curcode', :!inlinable, $T_OBJ, [], sub () {"$*CTX.codeRef"});
- add_simple_op('callercode', :!inlinable, $T_OBJ, [], sub () {"caller_ctx.codeRef"});
+ add_simple_op('curcode', :!inlinable, $T_OBJ, [], sub () {"$*CTX.codeRef()"});
+ add_simple_op('callercode', :!inlinable, $T_OBJ, [], sub () {"caller_ctx.codeRef()"});
# Native Call
add_simple_op('buildnativecall', $T_INT, [$T_OBJ, $T_STR, $T_STR, $T_STR, $T_OBJ, $T_OBJ], :sideffects, :ctx);
View
12 src/vm/js/nqp-runtime/runtime.js
@@ -47,7 +47,8 @@ loadOps(serialization);
var nativecall = require('./nativecall.js');
loadOps(nativecall);
-exports.CodeRef = require('./code-ref.js');
+var CodeRef = require('./code-ref.js');
+exports.CodeRef = CodeRef;
exports.CurLexpad = require('./curlexpad.js');
@@ -197,13 +198,16 @@ exports.toBool = function(arg, ctx) {
}
};
-function Ctx(callerCtx, outerCtx, codeRef) {
+function Ctx(callerCtx, outerCtx, callThis, codeRefAttr) {
this.caller = callerCtx;
this.outer = outerCtx;
- this.codeRef = codeRef;
+ this.callThis = callThis;
+ this.codeRefAttr = codeRefAttr;
}
-
+Ctx.prototype.codeRef = function() {
+ return (this.callThis instanceof CodeRef ? this.callThis : this.callThis[this.codeRefAttr]);
+};
Ctx.prototype.propagateException = function(exception) {
var ctx = this;
View
6 src/vm/js/nqp-runtime/serialization.js
@@ -356,7 +356,7 @@ BinaryWriteCursor.prototype.objRef = function(ref) {
console.log(ref);
console.trace("can't serialize this for sure");
console.log(typeof ref);
- console.log(ref.codeRef);
+ console.log(ref.codeRef());
process.exit();
}
if (!ref._SC) {
@@ -659,7 +659,7 @@ SerializationWriter.prototype.serializeSTable = function(st) {
SerializationWriter.prototype.serializeContext = function(ctx) {
/* Locate the static code ref this context points to. */
- var staticCodeRef = this.closureToStaticCodeRef(ctx.codeRef, true);
+ var staticCodeRef = this.closureToStaticCodeRef(ctx.codeRef(), true);
var staticCodeSC = staticCodeRef._SC;
if (staticCodeSC == null) {
throw 'Serialization Error: closure outer is a code object not in an SC';
@@ -711,7 +711,7 @@ SerializationWriter.prototype.serializeContext = function(ctx) {
SerializationWriter.prototype.getSerializedContextIdx = function(ctx) {
if (!ctx._SC) {
/* Make sure we should chase a level down. */
- if (this.closureToStaticCodeRef(ctx.codeRef, false) == null) {
+ if (this.closureToStaticCodeRef(ctx.codeRef(), false) == null) {
return 0;
}
else {

0 comments on commit 956c4ce

Please sign in to comment.