Skip to content

Commit af2caa2

Browse files
committed
persistent definitions via keepPreviouslyDeclaredValues rewriting
1 parent 9c4542b commit af2caa2

File tree

6 files changed

+174
-71
lines changed

6 files changed

+174
-71
lines changed

dist/lively.modules-with-lively.vm.js

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17165,8 +17165,10 @@ module.exports = function(acorn) {
1716517165
}, {
1716617166
key: "visitClassExpression",
1716717167
value: function visitClassExpression(node, scope, path) {
17168-
scope.classExprs.push(node);
17169-
scope.classExprPaths.push(path);
17168+
if (node.id) {
17169+
scope.classExprs.push(node);
17170+
scope.classExprPaths.push(path);
17171+
}
1717017172

1717117173
var visitor = this;
1717217174
// ignore id
@@ -19361,7 +19363,7 @@ var nodes = Object.freeze({
1936119363
var stmt = parsed.body[i];
1936219364
if (topLevel.classDecls.indexOf(stmt) !== -1) {
1936319365
if (options.declarationWrapper) {
19364-
parsed.body.splice(i, 1, assignExpr(options.captureObj, stmt.id, funcCall(options.declarationWrapper, literal(stmt.id.name), literal("class"), stmt, options.captureObj), false));
19366+
parsed.body.splice(i, 1, varDecl(stmt.id, assignExpr(options.captureObj, stmt.id, funcCall(options.declarationWrapper, literal(stmt.id.name), literal("class"), stmt, options.captureObj), false), "var"));
1936519367
} else {
1936619368
parsed.body.splice(i + 1, 0, assignExpr(options.captureObj, stmt.id, stmt.id, false));
1936719369
}
@@ -19512,7 +19514,7 @@ var nodes = Object.freeze({
1951219514
body.push(stmt);
1951319515
} else {
1951419516
body = body.concat(stmt.specifiers.map(function (specifier) {
19515-
return topLevel.declaredNames.indexOf(specifier.local.name) > -1 ? null : varDeclOrAssignment(parsed, {
19517+
return lively_lang.arr.include(topLevel.declaredNames, specifier.local.name) ? null : varDeclOrAssignment(parsed, {
1951619518
type: "VariableDeclarator",
1951719519
id: specifier.local,
1951819520
init: member(options.captureObj, specifier.local)
@@ -19776,6 +19778,8 @@ var capturing = Object.freeze({
1977619778
rewriteToRegisterModuleToCaptureSetters: rewriteToRegisterModuleToCaptureSetters
1977719779
});
1977819780

19781+
var defaultDeclarationWrapperName = "lively.capturing-declaration-wrapper";
19782+
1977919783
function evalCodeTransform(code, options) {
1978019784
// variable declaration and references in the the source code get
1978119785
// transformed so that they are bound to `varRecorderName` aren't local
@@ -19791,6 +19795,25 @@ var capturing = Object.freeze({
1979119795
// 2. capture top level vars into topLevelVarRecorder "environment"
1979219796
if (options.topLevelVarRecorder) {
1979319797

19798+
// 2.1 declare a function that should wrap all definitions, i.e. all var
19799+
// decls, functions, classes etc that get captured will be wrapped in this
19800+
// function. When using this with the option.keepPreviouslyDeclaredValues
19801+
// we will use a wrapping function that keeps the identity of prevously
19802+
// defined objects
19803+
19804+
var declarationWrapperName = options.declarationWrapperName || defaultDeclarationWrapperName;
19805+
if (options.keepPreviouslyDeclaredValues) {
19806+
options.declarationWrapper = {
19807+
type: "MemberExpression",
19808+
object: { type: "Identifier", name: options.varRecorderName },
19809+
property: { type: "Literal", value: declarationWrapperName },
19810+
computed: true
19811+
};
19812+
options.topLevelVarRecorder[declarationWrapperName] = declarationWrapperForKeepingValues;
19813+
}
19814+
19815+
// 2.2 Here we call out to the actual code transformation that installs the
19816+
// capture and wrap logic
1979419817
var blacklist = (options.dontTransform || []).concat(["arguments"]),
1979519818
undeclaredToTransform = !!options.recordGlobals ? null /*all*/ : lively_lang.arr.withoutAll(Object.keys(options.topLevelVarRecorder), blacklist);
1979619819

@@ -19833,7 +19856,39 @@ var capturing = Object.freeze({
1983319856
return result ? stringify(result) : code;
1983419857
}
1983519858

19859+
function copyProperties(source, target) {
19860+
var exceptions = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];
19861+
19862+
Object.getOwnPropertyNames(source).concat(Object.getOwnPropertySymbols(source)).forEach(function (name) {
19863+
return exceptions.indexOf(name) === -1 && Object.defineProperty(target, name, Object.getOwnPropertyDescriptor(source, name));
19864+
});
19865+
}
19866+
19867+
function declarationWrapperForKeepingValues(name, kind, value, recorder) {
19868+
if (kind === "function") return value;
19869+
19870+
if (kind === "class") {
19871+
var existingClass = recorder[name];
19872+
if (typeof existingClass === "function") {
19873+
copyProperties(value, existingClass, ["name", "length", "prototype"]);
19874+
copyProperties(value.prototype, existingClass.prototype);
19875+
return existingClass;
19876+
}
19877+
return value;
19878+
}
19879+
19880+
if (!value || (typeof value === "undefined" ? "undefined" : babelHelpers.typeof(value)) !== "object" || Array.isArray(value) || value.constructor === RegExp) return value;
19881+
19882+
if (recorder.hasOwnProperty(name)) {
19883+
copyProperties(value, recorder[name]);
19884+
return recorder[name];
19885+
}
19886+
19887+
return value;
19888+
}
19889+
1983619890
var evalSupport = Object.freeze({
19891+
defaultDeclarationWrapperName: defaultDeclarationWrapperName,
1983719892
evalCodeTransform: evalCodeTransform,
1983819893
evalCodeTransformOfSystemRegisterSetters: evalCodeTransformOfSystemRegisterSetters
1983919894
});
@@ -21363,7 +21418,8 @@ var categorizer = Object.freeze({
2136321418
topLevelVarRecorder: env.recorder,
2136421419
varRecorderName: env.recorderName,
2136521420
dontTransform: env.dontTransform,
21366-
recordGlobals: true
21421+
recordGlobals: true,
21422+
keepPreviouslyDeclaredValues: true
2136721423
},
2136821424
isGlobal = env.recorderName === "System.global",
2136921425
header = debug ? "console.log(\"[lively.modules] executing module " + fullname + "\");\n" : "",
@@ -21500,6 +21556,7 @@ var categorizer = Object.freeze({
2150021556
debug && console.log("[lively.modules customTranslate] Installing System.register setter captures for %s", load.name);
2150121557
translated = prepareTranslatedCodeForSetterCapture(translated, load.name, env, debug);
2150221558
}
21559+
2150321560
debug && console.log("[lively.modules customTranslate] done %s after %sms", load.name, Date.now() - start);
2150421561
return translated;
2150521562
});
@@ -21546,7 +21603,6 @@ var categorizer = Object.freeze({
2154621603
}
2154721604

2154821605
function instrumentSourceOfGlobalModuleLoad(System, load) {
21549-
2155021606
return System.translate(load).then(function (translated) {
2155121607
// return {localDeps: depNames, declare: declare};
2155221608
return { translated: translated };
@@ -23188,11 +23244,6 @@ var categorizer = Object.freeze({
2318823244
'use strict';
2318923245

2319023246
var babelHelpers = {};
23191-
babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
23192-
return typeof obj;
23193-
} : function (obj) {
23194-
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
23195-
};
2319623247

2319723248
babelHelpers.asyncToGenerator = function (fn) {
2319823249
return function () {
@@ -23436,38 +23487,6 @@ var categorizer = Object.freeze({
2343623487
var defaultTopLevelVarRecorderName = '__lvVarRecorder';
2343723488
var startEvalFunctionName = "lively.vm-on-eval-start";
2343823489
var endEvalFunctionName = "lively.vm-on-eval-end";
23439-
var declarationWrapperName = "lively.vm-declaration-wrapper";
23440-
function copyProperties(source, target) {
23441-
var exceptions = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];
23442-
23443-
Object.getOwnPropertyNames(source).concat(Object.getOwnPropertySymbols(source)).forEach(function (name) {
23444-
return exceptions.indexOf(name) === -1 && Object.defineProperty(target, name, Object.getOwnPropertyDescriptor(source, name));
23445-
});
23446-
}
23447-
23448-
function declarationWrapperForKeepingValues(name, kind, value, recorder) {
23449-
if (kind === "function") return value;
23450-
23451-
if (kind === "class") {
23452-
var existingClass = recorder[name];
23453-
if (typeof existingClass === "function") {
23454-
copyProperties(value, existingClass, ["name", "length", "prototype"]);
23455-
copyProperties(value.prototype, existingClass.prototype);
23456-
return existingClass;
23457-
}
23458-
return value;
23459-
}
23460-
23461-
if (!value || (typeof value === "undefined" ? "undefined" : babelHelpers.typeof(value)) !== "object" || Array.isArray(value) || value.constructor === RegExp) return value;
23462-
23463-
if (recorder.hasOwnProperty(name)) {
23464-
copyProperties(value, recorder[name]);
23465-
return recorder[name];
23466-
}
23467-
23468-
return value;
23469-
}
23470-
2347123490
function _normalizeEvalOptions(opts) {
2347223491
if (!opts) opts = {};
2347323492
opts = Object.assign({
@@ -23506,15 +23525,6 @@ var categorizer = Object.freeze({
2350623525
};
2350723526
}
2350823527

23509-
if (opts.keepPreviouslyDeclaredValues) {
23510-
opts.declarationWrapperFunction = declarationWrapperForKeepingValues;
23511-
opts.declarationWrapper = {
23512-
type: "MemberExpression",
23513-
object: { type: "Identifier", name: opts.varRecorderName },
23514-
property: { type: "Literal", value: declarationWrapperName },
23515-
computed: true
23516-
};
23517-
}
2351823528
return opts;
2351923529
}
2352023530

@@ -23598,10 +23608,6 @@ var categorizer = Object.freeze({
2359823608
};
2359923609
}
2360023610

23601-
if (options.declarationWrapperFunction) {
23602-
recorder[declarationWrapperName] = options.declarationWrapperFunction;
23603-
}
23604-
2360523611
// 2. Transform the code to capture top-level variables, inject function calls, ...
2360623612
try {
2360723613
code = evalCodeTransform(code, options);
@@ -23644,10 +23650,6 @@ var categorizer = Object.freeze({
2364423650
delete recorder[endEvalFunctionName];
2364523651
}
2364623652

23647-
if (options.declarationWrapperFunction) {
23648-
delete recorder[declarationWrapperName];
23649-
}
23650-
2365123653
if (err) {
2365223654
result.isError = true;result.value = err;
2365323655
} else result.value = value;

dist/lively.modules.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17165,8 +17165,10 @@ module.exports = function(acorn) {
1716517165
}, {
1716617166
key: "visitClassExpression",
1716717167
value: function visitClassExpression(node, scope, path) {
17168-
scope.classExprs.push(node);
17169-
scope.classExprPaths.push(path);
17168+
if (node.id) {
17169+
scope.classExprs.push(node);
17170+
scope.classExprPaths.push(path);
17171+
}
1717017172

1717117173
var visitor = this;
1717217174
// ignore id
@@ -19361,7 +19363,7 @@ var nodes = Object.freeze({
1936119363
var stmt = parsed.body[i];
1936219364
if (topLevel.classDecls.indexOf(stmt) !== -1) {
1936319365
if (options.declarationWrapper) {
19364-
parsed.body.splice(i, 1, assignExpr(options.captureObj, stmt.id, funcCall(options.declarationWrapper, literal(stmt.id.name), literal("class"), stmt, options.captureObj), false));
19366+
parsed.body.splice(i, 1, varDecl(stmt.id, assignExpr(options.captureObj, stmt.id, funcCall(options.declarationWrapper, literal(stmt.id.name), literal("class"), stmt, options.captureObj), false), "var"));
1936519367
} else {
1936619368
parsed.body.splice(i + 1, 0, assignExpr(options.captureObj, stmt.id, stmt.id, false));
1936719369
}
@@ -19512,7 +19514,7 @@ var nodes = Object.freeze({
1951219514
body.push(stmt);
1951319515
} else {
1951419516
body = body.concat(stmt.specifiers.map(function (specifier) {
19515-
return topLevel.declaredNames.indexOf(specifier.local.name) > -1 ? null : varDeclOrAssignment(parsed, {
19517+
return lively_lang.arr.include(topLevel.declaredNames, specifier.local.name) ? null : varDeclOrAssignment(parsed, {
1951619518
type: "VariableDeclarator",
1951719519
id: specifier.local,
1951819520
init: member(options.captureObj, specifier.local)
@@ -19776,6 +19778,8 @@ var capturing = Object.freeze({
1977619778
rewriteToRegisterModuleToCaptureSetters: rewriteToRegisterModuleToCaptureSetters
1977719779
});
1977819780

19781+
var defaultDeclarationWrapperName = "lively.capturing-declaration-wrapper";
19782+
1977919783
function evalCodeTransform(code, options) {
1978019784
// variable declaration and references in the the source code get
1978119785
// transformed so that they are bound to `varRecorderName` aren't local
@@ -19791,6 +19795,25 @@ var capturing = Object.freeze({
1979119795
// 2. capture top level vars into topLevelVarRecorder "environment"
1979219796
if (options.topLevelVarRecorder) {
1979319797

19798+
// 2.1 declare a function that should wrap all definitions, i.e. all var
19799+
// decls, functions, classes etc that get captured will be wrapped in this
19800+
// function. When using this with the option.keepPreviouslyDeclaredValues
19801+
// we will use a wrapping function that keeps the identity of prevously
19802+
// defined objects
19803+
19804+
var declarationWrapperName = options.declarationWrapperName || defaultDeclarationWrapperName;
19805+
if (options.keepPreviouslyDeclaredValues) {
19806+
options.declarationWrapper = {
19807+
type: "MemberExpression",
19808+
object: { type: "Identifier", name: options.varRecorderName },
19809+
property: { type: "Literal", value: declarationWrapperName },
19810+
computed: true
19811+
};
19812+
options.topLevelVarRecorder[declarationWrapperName] = declarationWrapperForKeepingValues;
19813+
}
19814+
19815+
// 2.2 Here we call out to the actual code transformation that installs the
19816+
// capture and wrap logic
1979419817
var blacklist = (options.dontTransform || []).concat(["arguments"]),
1979519818
undeclaredToTransform = !!options.recordGlobals ? null /*all*/ : lively_lang.arr.withoutAll(Object.keys(options.topLevelVarRecorder), blacklist);
1979619819

@@ -19833,7 +19856,39 @@ var capturing = Object.freeze({
1983319856
return result ? stringify(result) : code;
1983419857
}
1983519858

19859+
function copyProperties(source, target) {
19860+
var exceptions = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];
19861+
19862+
Object.getOwnPropertyNames(source).concat(Object.getOwnPropertySymbols(source)).forEach(function (name) {
19863+
return exceptions.indexOf(name) === -1 && Object.defineProperty(target, name, Object.getOwnPropertyDescriptor(source, name));
19864+
});
19865+
}
19866+
19867+
function declarationWrapperForKeepingValues(name, kind, value, recorder) {
19868+
if (kind === "function") return value;
19869+
19870+
if (kind === "class") {
19871+
var existingClass = recorder[name];
19872+
if (typeof existingClass === "function") {
19873+
copyProperties(value, existingClass, ["name", "length", "prototype"]);
19874+
copyProperties(value.prototype, existingClass.prototype);
19875+
return existingClass;
19876+
}
19877+
return value;
19878+
}
19879+
19880+
if (!value || (typeof value === "undefined" ? "undefined" : babelHelpers.typeof(value)) !== "object" || Array.isArray(value) || value.constructor === RegExp) return value;
19881+
19882+
if (recorder.hasOwnProperty(name)) {
19883+
copyProperties(value, recorder[name]);
19884+
return recorder[name];
19885+
}
19886+
19887+
return value;
19888+
}
19889+
1983619890
var evalSupport = Object.freeze({
19891+
defaultDeclarationWrapperName: defaultDeclarationWrapperName,
1983719892
evalCodeTransform: evalCodeTransform,
1983819893
evalCodeTransformOfSystemRegisterSetters: evalCodeTransformOfSystemRegisterSetters
1983919894
});
@@ -21363,7 +21418,8 @@ var categorizer = Object.freeze({
2136321418
topLevelVarRecorder: env.recorder,
2136421419
varRecorderName: env.recorderName,
2136521420
dontTransform: env.dontTransform,
21366-
recordGlobals: true
21421+
recordGlobals: true,
21422+
keepPreviouslyDeclaredValues: true
2136721423
},
2136821424
isGlobal = env.recorderName === "System.global",
2136921425
header = debug ? "console.log(\"[lively.modules] executing module " + fullname + "\");\n" : "",
@@ -21500,6 +21556,7 @@ var categorizer = Object.freeze({
2150021556
debug && console.log("[lively.modules customTranslate] Installing System.register setter captures for %s", load.name);
2150121557
translated = prepareTranslatedCodeForSetterCapture(translated, load.name, env, debug);
2150221558
}
21559+
2150321560
debug && console.log("[lively.modules customTranslate] done %s after %sms", load.name, Date.now() - start);
2150421561
return translated;
2150521562
});
@@ -21546,7 +21603,6 @@ var categorizer = Object.freeze({
2154621603
}
2154721604

2154821605
function instrumentSourceOfGlobalModuleLoad(System, load) {
21549-
2155021606
return System.translate(load).then(function (translated) {
2155121607
// return {localDeps: depNames, declare: declare};
2155221608
return { translated: translated };

dist/lively.modules_no-deps.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@
394394
topLevelVarRecorder: env.recorder,
395395
varRecorderName: env.recorderName,
396396
dontTransform: env.dontTransform,
397-
recordGlobals: true
397+
recordGlobals: true,
398+
keepPreviouslyDeclaredValues: true
398399
},
399400
isGlobal = env.recorderName === "System.global",
400401
header = debug ? "console.log(\"[lively.modules] executing module " + fullname + "\");\n" : "",
@@ -531,6 +532,7 @@
531532
debug && console.log("[lively.modules customTranslate] Installing System.register setter captures for %s", load.name);
532533
translated = prepareTranslatedCodeForSetterCapture(translated, load.name, env, debug);
533534
}
535+
534536
debug && console.log("[lively.modules customTranslate] done %s after %sms", load.name, Date.now() - start);
535537
return translated;
536538
});
@@ -577,7 +579,6 @@
577579
}
578580

579581
function instrumentSourceOfGlobalModuleLoad(System, load) {
580-
581582
return System.translate(load).then(function (translated) {
582583
// return {localDeps: depNames, declare: declare};
583584
return { translated: translated };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lively.modules",
3-
"version": "0.5.1",
3+
"version": "0.5.2",
44
"main": "dist/lively.modules.js",
55
"repository": "https://github.com/LivelyKernel/lively.modules",
66
"author": "Robert Krahn <[email protected]>",

0 commit comments

Comments
 (0)