mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2025-01-14 13:31:17 +00:00
scripting: io.mutable(), $ vs $$
This commit is contained in:
parent
005adcc9ba
commit
6343c75953
@ -51,6 +51,13 @@ class RuntimeError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setConstructorName(o: object) : void {
|
||||||
|
let name = Object.getPrototypeOf(o)?.constructor?.name;
|
||||||
|
if (name != null && name != 'Object') {
|
||||||
|
o[PROP_CONSTRUCTOR_NAME] = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class Environment {
|
export class Environment {
|
||||||
preamble: string;
|
preamble: string;
|
||||||
postamble: string;
|
postamble: string;
|
||||||
@ -82,7 +89,7 @@ export class Environment {
|
|||||||
}
|
}
|
||||||
print(args: any[]) {
|
print(args: any[]) {
|
||||||
if (args && args.length > 0 && args[0] != null) {
|
if (args && args.length > 0 && args[0] != null) {
|
||||||
this.obj[`$$print__${this.seq++}`] = args.length == 1 ? args[0] : args;
|
this.obj[`$print__${this.seq++}`] = args.length == 1 ? args[0] : args;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
preprocess(code: string): string {
|
preprocess(code: string): string {
|
||||||
@ -111,7 +118,7 @@ export class Environment {
|
|||||||
}
|
}
|
||||||
const convertTopToPrint = () => {
|
const convertTopToPrint = () => {
|
||||||
if (isTopLevel()) {
|
if (isTopLevel()) {
|
||||||
let printkey = `$$print__${this.seq++}`;
|
let printkey = `$print__${this.seq++}`;
|
||||||
update(`this.${printkey} = io.data.load(${source()}, ${JSON.stringify(printkey)})`);
|
update(`this.${printkey} = io.data.load(${source()}, ${JSON.stringify(printkey)})`);
|
||||||
//update(`print(${source()});`)
|
//update(`print(${source()});`)
|
||||||
}
|
}
|
||||||
@ -153,7 +160,7 @@ export class Environment {
|
|||||||
// literal comments
|
// literal comments
|
||||||
case 'Literal':
|
case 'Literal':
|
||||||
if (typeof node['value'] === 'string' && isTopLevel()) {
|
if (typeof node['value'] === 'string' && isTopLevel()) {
|
||||||
update(`this.$$doc__${this.seq++} = { literaltext: ${source()} };`);
|
update(`this.$doc__${this.seq++} = { literaltext: ${source()} };`);
|
||||||
} else {
|
} else {
|
||||||
convertTopToPrint();
|
convertTopToPrint();
|
||||||
}
|
}
|
||||||
@ -177,7 +184,8 @@ export class Environment {
|
|||||||
if (o == null) return;
|
if (o == null) return;
|
||||||
if (checked.has(o)) return;
|
if (checked.has(o)) return;
|
||||||
if (typeof o === 'object') {
|
if (typeof o === 'object') {
|
||||||
o[PROP_CONSTRUCTOR_NAME] = Object.getPrototypeOf(o).constructor.name;
|
setConstructorName(o);
|
||||||
|
delete o.$$callback; // clear callbacks (TODO? put somewhere else?)
|
||||||
if (o.length > 100) return; // big array, don't bother
|
if (o.length > 100) return; // big array, don't bother
|
||||||
if (o.BYTES_PER_ELEMENT > 0) return; // typed array, don't bother
|
if (o.BYTES_PER_ELEMENT > 0) return; // typed array, don't bother
|
||||||
checked.add(o); // so we don't recurse if cycle
|
checked.add(o); // so we don't recurse if cycle
|
||||||
@ -263,17 +271,11 @@ export class Environment {
|
|||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
getLoadableState() {
|
getLoadableState() {
|
||||||
let updated = null;
|
|
||||||
// TODO: use Loadable
|
|
||||||
// TODO: visit children?
|
// TODO: visit children?
|
||||||
// TODO: doesn't work
|
|
||||||
for (let [key, value] of Object.entries(this.obj)) {
|
for (let [key, value] of Object.entries(this.obj)) {
|
||||||
if (typeof value['$$getstate'] === 'function') {
|
|
||||||
let loadable = <any>value as io.Loadable;
|
let loadable = <any>value as io.Loadable;
|
||||||
if (updated == null) updated = {};
|
io.data.save(loadable, key);
|
||||||
updated[key] = loadable.$$getstate();
|
|
||||||
}
|
}
|
||||||
}
|
return io.$$getData();
|
||||||
return updated;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,6 @@ var $$cache: WeakMap<object,FileData> = new WeakMap();
|
|||||||
var $$store: WorkingStore;
|
var $$store: WorkingStore;
|
||||||
// backing store for data
|
// backing store for data
|
||||||
var $$data: {} = {};
|
var $$data: {} = {};
|
||||||
// events
|
|
||||||
var $$seq = 0;
|
|
||||||
|
|
||||||
export function $$setupFS(store: WorkingStore) {
|
export function $$setupFS(store: WorkingStore) {
|
||||||
$$store = store;
|
$$store = store;
|
||||||
@ -36,8 +34,6 @@ export namespace data {
|
|||||||
object.$$setstate(override);
|
object.$$setstate(override);
|
||||||
} else if (override) {
|
} else if (override) {
|
||||||
Object.assign(object, override);
|
Object.assign(object, override);
|
||||||
} else if (object.$$getstate) {
|
|
||||||
save(object, key); // $$reset not needed
|
|
||||||
}
|
}
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
@ -149,5 +145,17 @@ export class Mutable<T> implements Loadable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function mutable<T>(obj: object) : object {
|
export function mutable<T>(obj: object) : object {
|
||||||
return new Mutable(obj);
|
Object.defineProperty(obj, '$$setstate', {
|
||||||
|
value: function(newstate) {
|
||||||
|
Object.assign(this, newstate);
|
||||||
|
},
|
||||||
|
enumerable: false
|
||||||
|
});
|
||||||
|
Object.defineProperty(obj, '$$getstate', {
|
||||||
|
value: function() {
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
enumerable: false
|
||||||
|
});
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,7 @@ export class InteractionRecord implements io.Loadable {
|
|||||||
}
|
}
|
||||||
$$getstate() {
|
$$getstate() {
|
||||||
//TODO: this isn't always cleared before we serialize (e.g. if exception or move element)
|
//TODO: this isn't always cleared before we serialize (e.g. if exception or move element)
|
||||||
|
//and we do it in checkResult() too
|
||||||
this.$$callback = null;
|
this.$$callback = null;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user