mongodb-expressions
Version:
MongoDB expressions for fire.js
138 lines (126 loc) • 5.75 kB
JavaScript
var fire = require('fire')
var Runtime = fire.Runtime
var Expression = fire.Expression
Runtime.prototype._testOnly_runExpressionInstance = function(expressionInstance, block_context_base, context_block_overrides) {
//console.warn("block_context_base",block_context_base)
//console.warn("context_block_overrides",context_block_overrides)
/*if(expFunc === undefined || expFunc == null || typeof(expFunc) != 'function') {
throwInternalError("expFunc is required and must be a function")
}*/
if(expressionInstance === undefined || expressionInstance == null || !(expressionInstance instanceof Expression)) {
throwInternalError("expressionInstance is required to be an instance or derived from Expression")
}
if(block_context_base === undefined || typeof(block_context_base) != 'object' || block_context_base == null) {
throwInternalError("block_context_base is required and must be an non-null object")
}
if(context_block_overrides === undefined) {
throwInternalError("context_block_overrides must be an object or null")
}
if(block_context_base._loopCallback === undefined || block_context_base._loopCallback === null || typeof(block_context_base._loopCallback) != 'function') {
throwInternalError("block_context_base._loopCallback must be a function")
}
if(block_context_base._inputExpression === undefined || block_context_base._inputExpression === null || typeof(block_context_base._inputExpression) != 'function') {
throwInternalError("block_context_base._inputExpression must be a function")
}
if(block_context_base._variables === undefined || block_context_base._variables === null || typeof(block_context_base._variables) != 'object') {
throwInternalError("block_context_base._variables must be an object")
}
//console.warn("runExpressionFunc validation passed")
/*
// block_context_base members structure
{
_resultCallback: <Function>,
_loopCallback: <Function>,
_inputExpression: <Function>, // needs to be called with runExp
_variables: <Object>,
//_parentVariables: <Object>,
_hint: <Object> (optional),
_errorCallback: <Function>,
_parentResult: <Object>, // Caller Expression Block last Result
_result: <Object>
_parentContext: <Object>
}
*/
var _blockContext = {};
Object.keys(block_context_base).forEach(function(k) {
_blockContext[k] = block_context_base[k]
})
var localVariables = null;
var useSameScopeVariables = context_block_overrides == null || context_block_overrides._sameScope !== true
if(useSameScopeVariables) {
//
// If it's not running on the same scope, then copy all the variables.
//
localVariables = {}//Object.create(block_context_base._variables) //block_context_base._variables;
//console.warn("Copying Variables")
if(block_context_base._variables != undefined && block_context_base._variables != null) {
Object.keys(block_context_base._variables).forEach(function(k) {
//console.warn("Copying var ", k, " with value ", block_context_base._variables[k])
localVariables[k] = block_context_base._variables[k]
})
}
} else {
// Use the same Scope Varaibles
localVariables = block_context_base._variables
}
_blockContext._hint = undefined; //formality
if(context_block_overrides != null) {
for(var k in context_block_overrides) {
if(k == "_runtime" || k == "_parentVariables" || k == "_variables" || k == "_result" || k == "_parentContext" || k == "_errorInfo") continue; // can't replace these
_blockContext[k] = context_block_overrides[k]
}
}
_blockContext._variables = localVariables;
_blockContext._runtime = this;
_blockContext._result = undefined; //formality
_blockContext._parentResult = block_context_base._result
_blockContext._parentContext = block_context_base
_blockContext._rootExpression = undefined // _rootScope can not be inherited.
// The override key '_initialResult' can set the initial value of the calling expression block.
if(context_block_overrides && context_block_overrides._initialResult != null && context_block_overrides._initialResult != undefined) {
_blockContext._result = context_block_overrides._initialResult
}
expressionInstance._blockContext = _blockContext
expressionInstance.ensureInitialized()
// new Expressions model
var localVarsKeys = Object.keys(localVariables)
for(var i = 0; i < localVarsKeys.length; i++) {
expressionInstance.vars[localVarsKeys[i]] = localVariables[localVarsKeys[i]]
}
expressionInstance.resultCallback = function(res, parent) {
_blockContext._resultCallback(res)
}
expressionInstance.errorCallback = function(err) {
_blockContext._errorCallback(err)
}
expressionInstance.loopCallback = function(payload) {
_blockContext._loopCallback(payload)
}
expressionInstance.createInputExpression = function() {
var exp = new Expression()
exp.execute = function() {
var self = this
var pseudoExp = {
setResult: function(res) {
self.end(res)
},
execute: _blockContext._inputExpression
}
pseudoExp.execute()
}
return exp
}
expressionInstance.runtime = this
expressionInstance.hint = _blockContext._hint
expressionInstance.run() // run it
};
Runtime.prototype._testOnly_runExpressionByName = function(expressionName, base_context, context_overrides) {
//console.warn("Calling expression with name ", expressionName, " context_overrides ", context_overrides)
var expDefinition = this.loadedExpressionsMeta[expressionName]
if(expDefinition == undefined) {
throw new Error('JS1002', "Expression '" + expressionName + "' is not registered or was not loaded.");
}
var expObject = expDefinition.implementation
var expressionObject = new expObject()
this._testOnly_runExpressionInstance(expressionObject, base_context, context_overrides)
}