Skip to content

Commit

Permalink
vm2 v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
asiaziola committed Jul 31, 2023
1 parent d89488a commit cacb84e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion warp-contracts-plugin-vm2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "warp-contracts-plugin-vm2",
"version": "1.0.2",
"version": "1.0.3",
"description": "A VM2 plugin for warp contracts",
"types": "./lib/types/index.d.ts",
"main": "./lib/cjs/index.js",
Expand Down
69 changes: 68 additions & 1 deletion warp-contracts-plugin-vm2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import * as vm2 from 'vm2';
// https://github.com/evanw/esbuild/issues/1950
// eslint-disable-next-line
const BigNumber = require('bignumber.js');
function WeakMapError() {
this.init = function () {
throw new Error('WeakMap is blocked due to non-deterministic results.');
};
this.init();
}

function WeakRefError() {
this.init = function () {
throw new Error('WeakRef is blocked due to non-deterministic results.');
};
this.init();
}

export class VM2Plugin<State> implements WarpPlugin<VM2PluginInput, HandlerApi<State>> {
process(input: VM2PluginInput): HandlerApi<State> {
Expand All @@ -32,6 +45,7 @@ export class VM2Plugin<State> implements WarpPlugin<VM2PluginInput, HandlerApi<S
BigUint64Array: BigUint64Array,
TextEncoder: TextEncoder
};

const vm = new vm2.NodeVM({
console: 'off',
sandbox: {
Expand All @@ -43,7 +57,60 @@ export class VM2Plugin<State> implements WarpPlugin<VM2PluginInput, HandlerApi<S
if (!cond) throw new ContractError(message);
},
//https://github.com/patriksimek/vm2/issues/484#issuecomment-1327479592
...typedArrays
...typedArrays,
Math: (function (OriginalMath) {
const Math = Object.create(OriginalMath);

Math.random = () => {
throw new Error('test');
};

return Math;
})(Math),
Date: (function (OriginalDate) {
function Date(year, month, day, hours, minutes, seconds, ms) {
let date;

switch (arguments.length) {
case 0:
throw new Error('Date.now is blocked due to non-deterministic results.');

case 1:
date = new OriginalDate(year);
break;

default:
day = day || 1;
hours = hours || 0;
minutes = minutes || 0;
seconds = seconds || 0;
ms = ms || 0;
date = new OriginalDate(year, month, day, hours, minutes, seconds, ms);
break;
}

return date;
}

Date.parse = OriginalDate.parse;
Date.UTC = OriginalDate.UTC;
Date.toString = OriginalDate.toString;
Date.prototype = OriginalDate.prototype;

Date.now = function () {
throw new Error('Date.now is blocked due to non-deterministic results.');
};

return Date;
})(Date),
setTimeout: () => {
throw new Error('setTimeout is blocked due to non-deterministic results.');
},
setInterval: () => {
throw new Error('setInterval is blocked due to non-deterministic results.');
},
WeakMap: WeakMapError,
WeakRef: WeakRefError
},
compiler: 'javascript',
eval: false,
Expand Down

0 comments on commit cacb84e

Please sign in to comment.