diff --git a/packages/webamp-modern-2/src/maki/interpreter.tsx b/packages/webamp-modern-2/src/maki/interpreter.tsx index a5ba23e3..a8f88c11 100644 --- a/packages/webamp-modern-2/src/maki/interpreter.tsx +++ b/packages/webamp-modern-2/src/maki/interpreter.tsx @@ -146,14 +146,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -170,14 +170,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -195,14 +195,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -220,14 +220,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -304,15 +304,8 @@ class Interpreter { } const obj = this.stack.pop(); assert( - obj.type === "OBJECT", - "Tried to call a method on a primitive." - ); - assert( - typeof obj.value === "object", - "Trying to call a method on a not object" - ); - assert( - obj.value != null, + (obj.type === "OBJECT" && typeof obj.value) === "object" && + obj.value != null, "Guru Meditation: Tried to call method on null object" ); let value = obj.value[methodName](...methodArgs); @@ -326,12 +319,15 @@ class Interpreter { // variables[1] holds global NULL value value = this.variables[1]; } - if (returnType === "BOOL") { + if (returnType === "BOOLEAN") { assert(typeof value === "boolean", "BOOL should return a boolean"); value = value ? 1 : 0; } if (returnType === "OBJECT") { - assert(typeof value === "object", "not an object"); + assert( + typeof value === "object", + `Expected the returned value of ${klass.name}.${methodName} to be an object, but it was "${value}"` + ); } if (this.debug) { console.log(`Calling method ${methodName}`); @@ -379,7 +375,7 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to increment a non-number."); } @@ -394,7 +390,7 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to decrement a non-number."); } @@ -409,7 +405,7 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to increment a non-number."); } @@ -423,7 +419,7 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to increment a non-number."); } @@ -437,7 +433,7 @@ class Interpreter { const b = this.stack.pop(); switch (a.type) { case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error( `Tried to add non-numbers: ${b.type} + ${a.type}.` @@ -451,7 +447,7 @@ class Interpreter { } switch (b.type) { case "OBJECT": - case "BOOL": + case "BOOLEAN": throw new Error("Tried to add non-numbers."); } // TODO: Do we need to round the value if INT? @@ -465,14 +461,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -487,14 +483,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -509,14 +505,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": throw new Error("Tried to add non-numbers."); } // TODO: Do we need to round the value if INT? @@ -530,14 +526,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": throw new Error("Tried to add non-numbers."); // Need to coerce LHS if not int, RHS is always int (enforced by compiler) case "FLOAT": @@ -573,7 +569,7 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -588,14 +584,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } @@ -614,14 +610,14 @@ class Interpreter { switch (a.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } switch (b.type) { case "STRING": case "OBJECT": - case "BOOL": + case "BOOLEAN": case "NULL": throw new Error("Tried to add non-numbers."); } diff --git a/packages/webamp-modern-2/src/maki/objects.ts b/packages/webamp-modern-2/src/maki/objects.ts index adb8ba35..56e60b45 100644 --- a/packages/webamp-modern-2/src/maki/objects.ts +++ b/packages/webamp-modern-2/src/maki/objects.ts @@ -23,7 +23,7 @@ const objects: { [key: string]: ObjectDefinition } = { ...config, }; -export function getClass(id: String): ObjectDefinition { +export function getClass(id: string): ObjectDefinition { return normalizedObjects[getFormattedId(id)]; } @@ -35,9 +35,8 @@ export function getReturnType(classId: string, methodName: string): DataType { case "DOUBLE": case "STRING": case "FLOAT": - return upper as any; case "BOOLEAN": - return "BOOL"; + return upper as any; case "": return "NULL" as any; default: diff --git a/packages/webamp-modern-2/src/maki/parser.ts b/packages/webamp-modern-2/src/maki/parser.ts index 64875038..25610e0b 100644 --- a/packages/webamp-modern-2/src/maki/parser.ts +++ b/packages/webamp-modern-2/src/maki/parser.ts @@ -2,6 +2,7 @@ import { COMMANDS } from "./constants"; import { DataType, Variable } from "./v"; import MakiFile from "./MakiFile"; import { getReturnType } from "./objects"; +import { assert } from "../utils"; export type Command = { opcode: number; @@ -209,9 +210,13 @@ function readVariables({ makiFile, classes }) { let value = null; switch (typeName) { - // BOOLEAN + // BOOL case PRIMITIVE_TYPES[5]: value = uinit1; + assert( + value === 1 || value === 0, + "Expected boolean value to be initialized as zero or one" + ); break; // INT case PRIMITIVE_TYPES[2]: diff --git a/packages/webamp-modern-2/src/maki/v.ts b/packages/webamp-modern-2/src/maki/v.ts index 312992fb..f9f24287 100644 --- a/packages/webamp-modern-2/src/maki/v.ts +++ b/packages/webamp-modern-2/src/maki/v.ts @@ -2,8 +2,8 @@ import BaseObject from "../skin/BaseObject"; export type Variable = | { - type: "BOOL"; - value: boolean; + type: "BOOLEAN"; + value: number; // 1 || 0 } | { type: "INT" | "FLOAT" | "DOUBLE"; @@ -36,4 +36,7 @@ export const V = { newInt(value: number | boolean): Variable { return { type: "INT", value: Number(value) }; }, + newBool(value: boolean): Variable { + return { type: "BOOLEAN", value: value ? 1 : 0 }; + }, }; diff --git a/packages/webamp-modern-2/src/skin/Button.ts b/packages/webamp-modern-2/src/skin/Button.ts index 2f6009b2..0bded5e6 100644 --- a/packages/webamp-modern-2/src/skin/Button.ts +++ b/packages/webamp-modern-2/src/skin/Button.ts @@ -1,5 +1,6 @@ import GuiObj from "./GuiObj"; import UI_ROOT from "../UIRoot"; +import { V } from "../maki/v"; // http://wiki.winamp.com/wiki/XML_GUI_Objects#.3Cbutton.2F.3E_.26_.3Ctogglebutton.2F.3E export default class Button extends GuiObj { @@ -84,9 +85,7 @@ export default class Button extends GuiObj { if (onoff !== this._active) { this._active = onoff; - UI_ROOT.vm.dispatch(this, "onactivate", [ - { type: "BOOL", value: onoff ? 1 : 0 }, - ]); + UI_ROOT.vm.dispatch(this, "onactivate", [V.newBool(onoff)]); } }