You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.0 KiB
JavaScript

"use strict";
const measureTime = require("./astformer/util/measure-time");
const transpile = require("./transpile");
module.exports = function evaluate(nixCode) {
let transpiled = transpile(nixCode);
const api = {
builtins: {},
$$jsNix$memoize: function (func) {
let isCalled = false;
let storedResult;
return function (arg) {
if (isCalled === false) {
storedResult = func(arg);
isCalled = true;
}
return storedResult;
};
}
};
// TODO: Switch to Node `vm` API instead, and check whether there's a polyfill for it for non-Node environments, build a custom one if not
const context = { module: {}, exports: {} };
context.module.exports = exports;
new Function("module", transpiled)(context.module);
// Warm-up for hot VM performance testing
// for (let i = 0; i < 10000; i++) {
// context.module.exports(api);
// }
let result = measureTime(() => context.module.exports(api));
if (process.env.DEBUG_NIX) {
console.log("-- EVALUATION RESULT:");
console.log(result);
}
return result;
};