From efbb5c6d482b3bb0db22058369cfff39abe3d747 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Sun, 16 Jun 2019 12:39:55 -0700 Subject: [PATCH] Attach function objects to function names --- experiments/maki-interpreter/index.js | 6 ++-- experiments/maki-interpreter/index.test.js | 1 + experiments/maki-interpreter/objects.js | 33 +++++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/experiments/maki-interpreter/index.js b/experiments/maki-interpreter/index.js index c966ff5a..eaadc6ac 100644 --- a/experiments/maki-interpreter/index.js +++ b/experiments/maki-interpreter/index.js @@ -1,6 +1,6 @@ const { COMMANDS } = require("./constants"); const Command = require("./command"); -const { getClass } = require("./objects"); +const { getClass, getObjectFunction } = require("./objects"); const MAGIC = "FG"; const ENCODING = "binary"; @@ -47,10 +47,12 @@ class Parser { const typeOffset = classCode & 0xff; const dummy2 = this._readUInt16LE(); const name = this._readString(); + const klass = types[typeOffset]; functionNames.push({ dummy2, name, - class: types[typeOffset] + class: klass, + function: getObjectFunction(klass, name) }); } return functionNames; diff --git a/experiments/maki-interpreter/index.test.js b/experiments/maki-interpreter/index.test.js index 70738dd1..392ab03f 100644 --- a/experiments/maki-interpreter/index.test.js +++ b/experiments/maki-interpreter/index.test.js @@ -71,6 +71,7 @@ describe("standardframe.maki", () => { "init" ]); expect(maki.functionNames.every(func => func.class != null)).toBe(true); + expect(maki.functionNames.every(func => func.function != null)).toBe(true); }); test("can read variables", () => { diff --git a/experiments/maki-interpreter/objects.js b/experiments/maki-interpreter/objects.js index dfa9ecb4..1982dfc2 100644 --- a/experiments/maki-interpreter/objects.js +++ b/experiments/maki-interpreter/objects.js @@ -3933,6 +3933,23 @@ Object.keys(objects).forEach(key => { normalizedObjects[key.toLowerCase()] = objects[key]; }); +const objectsByName = {}; +Object.values(objects).forEach(object => { + objectsByName[object.name] = object; +}); + +Object.values(normalizedObjects).forEach(object => { + const parentClass = objectsByName[object.parent]; + if (parentClass == null) { + if (object.parent === "@{00000000-0000-0000-0000-000000000000}@") { + } else { + console.log(`Could not find parent class named ${object.parent}`); + throw new Error("wat"); + } + } + object.parentClass = parentClass; +}); + function getClass(id) { // https://en.wikipedia.org/wiki/Universally_unique_identifier#Encoding const formattedId = id.replace( @@ -3943,4 +3960,18 @@ function getClass(id) { return normalizedObjects[formattedId.toLowerCase()]; } -module.exports = { getClass }; +function getObjectFunction(klass, functionName) { + const method = klass.functions.find(func => { + // TODO: This could probably be normalized at load time, or evern sooner. + return func.name.toLowerCase() === functionName.toLowerCase(); + }); + if (method != null) { + return method; + } + if (klass.parentClass == null) { + throw new Error(`Could not find method ${functionName} on ${klass.name}.`); + } + return getObjectFunction(klass.parentClass, functionName); +} + +module.exports = { getClass, getObjectFunction };