Skip to content

Commit

Permalink
initial pass
Browse files Browse the repository at this point in the history
initial pass to fix the function init problem, involving a redesign of the code execution. Need to go back and clean and test.
  • Loading branch information
sutter-dave committed Jan 20, 2018
1 parent 37fda28 commit 05b3a71
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/web/node_modules/
/web/apogeeapp/test-electron/node_modules/
/web/apogeeapp/test-electron/npm-debug.log
57 changes: 45 additions & 12 deletions web/apogee/data/FunctionTable.js
Expand Up @@ -28,21 +28,54 @@ apogee.base.mixin(apogee.FunctionTable,apogee.Codeable);
// Codeable Methods
//------------------------------

apogee.FunctionTable.prototype.processMemberFunction = function(memberFunction) {
this.setData(memberFunction);
}
apogee.FunctionTable.prototype.processMemberFunction = function(memberGenerator) {
var instance = this;

var packagerFunction = function() {
var src = {};
var memberInitialized = false;
var memberFunction = null;
var issue = null;


var initMember = function() {
memberInitialized = true;
var impactorSuccess = instance.memberFunctionInitialize();
if(impactorSuccess) {
memberFunction = memberGenerator();
}
else {
if(instance.hasError()) {
issue = new Error("Error in dependency: " + instance.getFullName());

apogee.FunctionTable.prototype.postInitializeAction = function() {
//pending check - we don't know if a function is pending until we
//actually call it. I didn't know how else to capture this in the
//calling code other than use an error. But this is not an error
if(this.hasError()) {
throw new Error("Error in dependency: " + this.getFullName());
}
if(instance.getResultPending()) {
issue = apogee.Codeable.MEMBER_FUNCTION_PENDING;
}
else {
issue = new Error("Unknown problem in initializing: " + instance.getFullName());
}
}
}

var wrapperMemberFunction = function(argList) {
if(!memberInitialized) {
initMember();
}

if(memberFunction) {
return memberFunction.apply(null,arguments);
}
else {
throw issue;
}
}

return wrapperMemberFunction;
}
if(this.getResultPending()) {
throw apogee.Codeable.MEMBER_FUNCTION_PENDING;
}

var memberFunction = packagerFunction();
this.setData(memberFunction);
}

//------------------------------
Expand Down
17 changes: 14 additions & 3 deletions web/apogee/data/JsonTable.js
Expand Up @@ -49,10 +49,21 @@ apogee.JsonTable.prototype.getArgList = function() {
return [];
}

apogee.JsonTable.prototype.processMemberFunction = function(memberFunction) {
apogee.JsonTable.prototype.processMemberFunction = function(memberGenerator) {

//the data is the output of the function
var data = memberFunction();
//first initialize
var initialized = this.memberFunctionInitialize();

var data;
if(initialized) {
//the data is the output of the function
var memberFunction = memberGenerator();
data = memberFunction();
}
else {
//initialization issue = error or pending dependancy
data = undefined;
}

//if the return value is a Promise, the data is asynch
if(apogee.base.isPromise(data)) {
Expand Down
28 changes: 10 additions & 18 deletions web/apogee/datacomponents/Codeable.js
Expand Up @@ -25,7 +25,7 @@ apogee.Codeable.init = function(argList) {
this.varInfo = null;
this.dependencyInfo = null;
this.memberFunctionInitializer = null;
this.memberFunction = null;
this.memberGenerator = null;
this.codeErrors = [];

this.clearCalcPending();
Expand Down Expand Up @@ -87,15 +87,14 @@ apogee.Codeable.setCodeInfo = function(codeInfo) {
//set the code by exectuing generator
try {
//get the inputs to the generator
var instance = this;
var initFunction = function() {
return instance.memberFunctionInitialize();
}
var messenger = new apogee.action.Messenger(this);

//get the generated fucntion
var generatedFunctions = codeInfo.generatorFunction(initFunction,messenger);
this.memberFunction = generatedFunctions.memberFunction;
var generatedFunctions = codeInfo.generatorFunction(messenger);
//-------------------------------------------------------------
//this is a standin for memeber generator before we fix the code compiler
this.memberGenerator = generatedFunctions.memberGenerator;
//-------------------------------------------------------------
this.memberFunctionInitializer = generatedFunctions.initializer;

this.codeErrors = [];
Expand All @@ -111,7 +110,7 @@ apogee.Codeable.setCodeInfo = function(codeInfo) {

if(this.codeErrors.length > 0) {
//code not valid
this.memberFunction = null;
this.memberGenerator = null;
this.memberFunctionInitializer = null;
}
this.codeSet = true;
Expand Down Expand Up @@ -164,7 +163,7 @@ apogee.Codeable.clearCode = function() {
this.varInfo = null;
this.dependencyInfo = null;
this.memberFunctionInitializer = null;
this.memberFunction = null;
this.memberGenerator = null;
this.codeErrors = [];

this.clearCalcPending();
Expand Down Expand Up @@ -202,7 +201,7 @@ apogee.Codeable.calculate = function() {
return;
}

if((!this.memberFunction)||(!this.memberFunctionInitializer)) {
if((!this.memberGenerator)||(!this.memberFunctionInitializer)) {
var msg = "Function not found for member: " + this.getName();
var actionError = new apogee.ActionError(msg,"Codeable - Calculate",this);
this.addError(actionError);
Expand All @@ -211,7 +210,7 @@ apogee.Codeable.calculate = function() {
}

try {
this.processMemberFunction(this.memberFunction);
this.processMemberFunction(this.memberGenerator);
}
catch(error) {
if(error == apogee.Codeable.MEMBER_FUNCTION_PENDING) {
Expand Down Expand Up @@ -284,13 +283,6 @@ apogee.Codeable.memberFunctionInitialize = function() {
this.addError(actionError);
this.initReturnValue = false;
}
finally {
//This lets us do something outside the catch block - mainly just to
//let some things throw the (cludgy) pending error.
if(this.postInitializeAction) {
this.postInitializeAction();
}
}

this.calcInProgress = false;
this.functionInitialized = true;
Expand Down
10 changes: 6 additions & 4 deletions web/apogee/lib/codeCompiler.js
Expand Up @@ -127,7 +127,7 @@ apogee.codeCompiler.createGeneratorFunction = function(varInfo, combinedFunction
combinedFunctionBody
);

var generatorFunction = new Function("__initFunction","apogeeMessenger",generatorBody);
var generatorFunction = new Function("apogeeMessenger",generatorBody);
return generatorFunction;
}

Expand All @@ -151,7 +151,6 @@ apogee.codeCompiler.MEMBER_FUNCTION_FORMAT_TEXT = [
"//member function----------------",
"function __memberFunction({1}) {",
"//overhead code",
"if(!__initFunction()) return undefined;",
"__memberFunctionDebugHook();",
"",
"//user code",
Expand All @@ -163,7 +162,7 @@ apogee.codeCompiler.MEMBER_FUNCTION_FORMAT_TEXT = [
/** This line is added when getting the dependencies to account for some local
* variables in the member function.
* @private */
apogee.codeCompiler.MEMBER_LOCALS_TEXT = "var __initFunction, apogeeMessenger, __memberFunction, __memberFunctionDebugHook;";
apogee.codeCompiler.MEMBER_LOCALS_TEXT = "var apogeeMessenger, __memberFunction, __memberFunctionDebugHook;";

/** This is the format string to create the code body for the object function
* Input indices:
Expand All @@ -181,9 +180,12 @@ apogee.codeCompiler.GENERATOR_FUNCTION_FORMAT_TEXT = [
"{1}};",
"",
"//user code",
"function __memberGenerator() {",
"{2}",
"return __memberFunction",
"}",
"return {",
"'memberFunction': __memberFunction,",
"'memberGenerator': __memberGenerator,",
"'initializer': __initializer",
"};"
].join("\n");
Expand Down

0 comments on commit 05b3a71

Please sign in to comment.