MCPcopy Create free account
hub / github.com/apache/pouchdb / evalFunctionInVm

Function evalFunctionInVm

lib/index.js:7963–7991  ·  view source on GitHub ↗
(func, emit)

Source from the content-addressed store, hash-verified

7961// All of this vm hullaballoo is to be able to run arbitrary code in a sandbox
7962// for security reasons.
7963function evalFunctionInVm(func, emit) {
7964 return function (arg1, arg2, arg3) {
7965 var code = '(function() {"use strict";' +
7966 'var createBuiltInError = ' + createBuiltInErrorInVm.toString() + ';' +
7967 'var sum = ' + sum.toString() + ';' +
7968 'var log = function () {};' +
7969 'var isArray = Array.isArray;' +
7970 'var toJSON = JSON.parse;' +
7971 'var __emitteds__ = [];' +
7972 'var emit = function (key, value) {__emitteds__.push([key, value]);};' +
7973 'var __result__ = (' +
7974 func.replace(/;\s*$/, '') + ')' + '(' +
7975 JSON.stringify(arg1) + ',' +
7976 JSON.stringify(arg2) + ',' +
7977 JSON.stringify(arg3) + ');' +
7978 'return {result: __result__, emitteds: __emitteds__};' +
7979 '})()';
7980
7981 var output = vm.runInNewContext(code);
7982
7983 output.emitteds.forEach(function (emitted) {
7984 emit(emitted[0], emitted[1]);
7985 });
7986 if (isBuiltInError(output.result)) {
7987 output.result = convertToTrueError(output.result);
7988 }
7989 return output.result;
7990 };
7991}
7992
7993var log = guardedConsole.bind(null, 'log');
7994var toJSON = JSON.parse;

Callers

nothing calls this directly

Calls 4

emitFunction · 0.70
isBuiltInErrorFunction · 0.70
convertToTrueErrorFunction · 0.70
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…