Make the VM sync

This commit is contained in:
Jordan Eldredge 2019-07-23 16:53:20 -07:00
parent 0e1f94b245
commit 41553c5a85
7 changed files with 25 additions and 47 deletions

View file

@ -17,12 +17,12 @@ class Emitter {
};
}
async trigger(eventName, ...args) {
await Promise.all(this._globalHooks.map(cb => cb(eventName, args)));
trigger(eventName, ...args) {
this._globalHooks.map(cb => cb(eventName, args));
if (this._hooks[eventName] == null) {
return;
}
await Promise.all(this._hooks[eventName].map(cb => cb(...args)));
this._hooks[eventName].map(cb => cb(...args));
}
listenToAll(cb) {

View file

@ -3,7 +3,7 @@ const { getClass, getFormattedId } = require("./objects");
const interpret = require("./virtualMachine");
const { printCommand } = require("./prettyPrinter");
async function main({ runtime, data, system, log, logger }) {
function main({ runtime, data, system, log, logger }) {
const program = parse(data);
// Replace class hashes with actual JavaScript classes from the runtime
@ -29,10 +29,8 @@ async function main({ runtime, data, system, log, logger }) {
// const logger = log ? printCommand : logger;
// TODO: Handle disposing of this.
// TODO: Handle passing in variables.
variable.hook(method.name, async () => {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await interpret(commandOffset, program, [], { logger });
variable.hook(method.name, () => {
interpret(commandOffset, program, [], { logger });
});
});
@ -44,7 +42,7 @@ async function main({ runtime, data, system, log, logger }) {
// Set the System global
// TODO: We could confirm that this variable has the "system" flag set.
program.variables[0].setValue(system);
await system.js_start();
system.js_start();
}
module.exports = main;

View file

@ -5,13 +5,11 @@ const runtime = require("../runtime");
const interpret = require("./interpreter");
const { VERSIONS } = require("./testConstants");
async function runFile(version, fileName) {
function runFile(version, fileName) {
const relativePath = `../../resources/maki_compiler/${version}/${fileName}`;
const system = new System();
const data = fs.readFileSync(path.join(__dirname, relativePath));
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await interpret({ runtime, data, system, log: false });
interpret({ runtime, data, system, log: false });
}
// The basicTest.m file that jberg prepared follows a convention for what a
@ -40,10 +38,8 @@ describe("can call messageBox with hello World", () => {
VERSIONS.WINAMP_5_66,
];
versions.forEach(version => {
test(`with bytecode compiled by ${version}`, async () => {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await runFile(version, "hello_world.maki");
test(`with bytecode compiled by ${version}`, () => {
runFile(version, "hello_world.maki");
expect(mockMessageBox).toHaveBeenCalledTimes(1);
expect(mockMessageBox).toHaveBeenCalledWith(
"Hello World",
@ -66,10 +62,8 @@ describe("can use basic operators", () => {
];
versions.forEach(version => {
test(`with basic test bytecode compiled by ${version}`, async () => {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await runFile(version, "basicTests.maki");
test(`with basic test bytecode compiled by ${version}`, () => {
runFile(version, "basicTests.maki");
expect(mockMessageBox.mock.calls).toEqual(
[
"2 + 2 = 4",
@ -147,10 +141,8 @@ describe("can use simple functions", () => {
];
versions.forEach(version => {
test(`with bytecode compiled by ${version}`, async () => {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await runFile(version, "simpleFunctions.maki");
test(`with bytecode compiled by ${version}`, () => {
runFile(version, "simpleFunctions.maki");
expect(mockMessageBox.mock.calls).toEqual(
[
"simple custom function",

View file

@ -17,13 +17,9 @@ class Variable {
this._unsubscribeFromValue();
}
if (this.typeName === "OBJECT") {
this._unsubscribeFromValue = value.js_listenToAll(
async (eventName, args) => {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await this._emitter.trigger(eventName, args);
}
);
this._unsubscribeFromValue = value.js_listenToAll((eventName, args) => {
this._emitter.trigger(eventName, args);
});
}
this._value = value;
}

View file

@ -10,7 +10,7 @@ function coerceTypes(var1, var2, val1, val2) {
return val1;
}
async function interpret(start, program, stack = [], { logger = null }) {
function interpret(start, program, stack = [], { logger = null }) {
const { commands, methods, variables, classes } = program;
function twoArgCoercingOperator(operator) {
@ -37,9 +37,7 @@ async function interpret(start, program, stack = [], { logger = null }) {
const command = commands[i];
// Print some debug info
if (logger) {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await logger({ i, command, stack, variables, program });
logger({ i, command, stack, variables, program });
}
switch (command.opcode) {
@ -144,9 +142,7 @@ async function interpret(start, program, stack = [], { logger = null }) {
// callGlobal
case 25: {
const offset = command.arg;
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
const value = await interpret(offset, program, stack, {
const value = interpret(offset, program, stack, {
logger,
});
stack.push(value);

View file

@ -5,10 +5,8 @@ class MakiObject {
this._emitter = new Emitter();
}
async js_trigger(eventName, ...args) {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await this._emitter.trigger(eventName, args);
js_trigger(eventName, ...args) {
this._emitter.trigger(eventName, args);
}
js_listenToAll(cb) {

View file

@ -12,10 +12,8 @@ class System extends MakiObject {
return "System";
}
async js_start() {
// Remove this await when we can run the VM synchronously.
// See GitHub issue #814
await this.js_trigger("onScriptLoaded");
js_start() {
this.js_trigger("onScriptLoaded");
}
getScriptGroup() {