react-native-integrate
Version:
Automate integration of additional code into React Native projects
50 lines (49 loc) • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processScript = processScript;
const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;
function processScript(script, _variables, inline, async, ctx) {
const functionSet = {
get: function (variable) {
return _variables.get(variable);
},
set: function (variable, value) {
_variables.set(variable, value);
},
json: function (object) {
return JSON.stringify(object, null, 2);
},
parse: function (str) {
return JSON.parse(str);
},
require,
};
const scope = Object.assign(Object.keys(globalThis).reduce((acc, k) => {
acc[k] = undefined;
return acc;
}, {}), functionSet, ctx);
for (const storeKey in _variables.getStore()) {
Object.defineProperty(scope, storeKey, {
get() {
return _variables.get(storeKey);
},
set(value) {
return _variables.set(storeKey, value);
},
});
}
if (async) {
if (inline) {
return new AsyncFunction('script', 'with(this) { return eval(script) }').call(scope, script);
}
else {
return new AsyncFunction('with(this) { ' + script + ' }').call(scope);
}
}
if (inline) {
return new Function('script', 'with(this) { return eval(script) }').call(scope, script);
}
else {
return new Function('with(this) { ' + script + ' }').call(scope);
}
}