Skip to content

Commit

Permalink
issue #6 Fix for modifier plugin name and variable name is same and t…
Browse files Browse the repository at this point in the history
…hey conflict

To avoid conflicts do not use regular function execute which evals the code with with statement.
Create a new function which calls function by object.

Reference:
#6
https://code.google.com/p/jsmart/issues/detail?id=21
  • Loading branch information
Uma committed Dec 25, 2014
1 parent edd5516 commit 801c8e4
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions jsmart.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@
return code;
}

/**
* Execute function when we have a object.
*
* @param object obj Object of the function to be called.
* @param array args Arguments to pass to a function.
*
* @return
* @throws Error If function obj does not exists.
*/
function executeByFuncObject(obj, args) {
try {
return obj.apply(this, args);
} catch (e) {
throw new Error(e.message + ' in \n' + code);
}
}

function assignVar(nm, val, data)
{
if (nm.match(/\[\]$/)) //ar[] =
Expand Down Expand Up @@ -2089,17 +2106,24 @@
jSmart.prototype.registerPlugin(
'function',
'__func',
function(params, data)
{
var paramNames = [];
var paramValues = {};
for(var i=0; i<params.length; ++i)
{
function(params, data) {
var paramNames = [], paramValues = {}, paramData = [];
for (var i=0; i<params.length; ++i) {
paramNames.push(params.name+'__p'+i);
paramData.push(params[i]);
paramValues[params.name+'__p'+i] = params[i];
}
var fname = ('__owner' in data && params.name in data.__owner) ? ('__owner.'+params.name) : params.name;
return execute(fname + '(' + paramNames.join(',') + ')', obMerge({},data,paramValues));
var fname, mergedParams = obMerge({}, data, paramValues);
if (('__owner' in data && params.name in data.__owner)) {
fname = '__owner.'+params.name;
return execute(fname + '(' + paramNames.join(',') + ')', mergedParams);
} else if (modifiers.hasOwnProperty(params.name)) {
fname = modifiers[params.name]
return executeByFuncObject(fname, paramData, mergedParams);
} else {
fname = params.name;
return execute(fname + '(' + paramNames.join(',') + ')', mergedParams);
}
}
);

Expand Down

0 comments on commit 801c8e4

Please sign in to comment.