Skip to content

Commit

Permalink
vm: Add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
qti3e committed Aug 16, 2019
1 parent 96c4d21 commit 83f1923
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
9 changes: 4 additions & 5 deletions jsc/vm/call.ts
Expand Up @@ -25,6 +25,7 @@ import { Scope } from "./scope";
import { ByteCode, byteCodeArgSize } from "../src/bytecode";
import { Obj } from "./obj";
import { compiler } from "./compiler";
import { timer } from "./timer";

export function call(callable: Value, env: Value, args: Value[] = []): Value {
if (!isCallable(callable)) {
Expand All @@ -42,13 +43,14 @@ export function exec(
data: CompiledData,
callScope: Scope,
env: Value,
args: Value[] = []
args: Value[] = [],
dataStack: DataStack = new DataStack()
): Value {
const dataStack = new DataStack();
const { codeSection, constantPool, scope } = data;
let cursor = 0;

while (cursor < codeSection.size) {
timer.check();
const bytecode = codeSection.get(cursor) as ByteCode;
const argsSize = byteCodeArgSize[bytecode] || 0;
let nextCursor = cursor + argsSize + 1;
Expand Down Expand Up @@ -426,9 +428,6 @@ export function exec(
dataStack.push(ret);
break;
}

default:
console.log("TODO: " + ByteCode[bytecode]);
}
cursor = nextCursor;
}
Expand Down
6 changes: 5 additions & 1 deletion jsc/vm/ds.ts
Expand Up @@ -9,7 +9,11 @@
import { Data } from "./data";

export class DataStack {
private stack: Data[] = [];
private readonly stack: Data[] = [];

getSize(): number {
return this.stack.length;
}

push(value: Data): void {
this.stack.push(value);
Expand Down
18 changes: 18 additions & 0 deletions jsc/vm/timer.ts
@@ -0,0 +1,18 @@
class Timer {
private timeout = -1;
private currentStart = 0;

check(): void {
if (this.timeout < 0) return;

if (Date.now() - this.currentStart > this.timeout)
throw new Error("Timeout");
}

start(timeout: number): void {
this.currentStart = Date.now();
this.timeout = timeout;
}
}

export const timer = new Timer();
26 changes: 20 additions & 6 deletions jsc/vm/vm.ts
Expand Up @@ -10,16 +10,30 @@ import { Scope } from "./scope";
import { exec } from "./call";
import { Value } from "./data";
import { compiler } from "./compiler";
import { DataStack } from "./ds";
import { timer } from "./timer";

export interface VMOptions {
timeout: number;
}

export class VM {
private scope: Scope = new Scope(false);
readonly scope: Scope = new Scope(false);
readonly dataStack: DataStack = new DataStack();
readonly timeout: number;

constructor(options?: VMOptions) {
options = {
timeout: -1,
...options
};

this.timeout = options.timeout;
}

compileAndExec(source: string): Value {
const main = compiler.compile(source);

console.time("exec");
const ret = exec(main, this.scope, this.scope.obj, []);
console.timeEnd("exec");
return ret;
timer.start(this.timeout);
return exec(main, this.scope, this.scope.obj, [], this.dataStack);
}
}

0 comments on commit 83f1923

Please sign in to comment.