Skip to content

Commit

Permalink
Lily/TempVariables2: nitpicking (#1796)
Browse files Browse the repository at this point in the history
Safari 13 and 14 have enough users that they show up in the top 100 user
agents on clouddata.turbowarp.org so I'd like to keep as much alive
there for as long as possible. Thus avoid using ??= (Safari 14) or
Object.hasOwn (Safari 15.4).

Also includes ESLint rules, so that these are detected automatically
moving forward.

`ext_${extensionId}` is also a TurboWarp convention not a Scratch
convention

Probably at some point it makes sense to just adopt babel and compile
extensions down to Safari 12 but for now, avoiding significant
transformations is nice
  • Loading branch information
GarboMuffin authored Dec 21, 2024
1 parent 9f7c30b commit 0f38017
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ module.exports = [
],
'no-restricted-syntax': [
'error',
{
selector: 'AssignmentExpression[operator="??="]',
message: 'x ??= y syntax is too new; use x = x ?? y intead'
},
{
selector: 'MemberExpression[object.name=Object][property.name=hasOwn]',
message: 'Object.hasOwn(...) is too new; use Object.prototype.hasOwnProperty.call(...) instead'
},
{
selector: 'CallExpression[callee.name=fetch]',
message: 'Use Scratch.fetch() instead of fetch()'
Expand Down
42 changes: 30 additions & 12 deletions extensions/Lily/TempVariables2.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,17 @@

setThreadVariable(args, util) {
const thread = util.thread;
thread.variables ??= Object.create(null);
if (!thread.variables) {
thread.variables = Object.create(null);
}
thread.variables[args.VAR] = args.STRING;
}

changeThreadVariable(args, util) {
const thread = util.thread;
thread.variables ??= Object.create(null);
if (!thread.variables) {
thread.variables = Object.create(null);
}
const vars = thread.variables;
const prev = Scratch.Cast.toNumber(vars[args.VAR]);
const next = Scratch.Cast.toNumber(args.NUM);
Expand All @@ -232,21 +236,29 @@

getThreadVariable(args, util) {
const thread = util.thread;
thread.variables ??= Object.create(null);
if (!thread.variables) {
thread.variables = Object.create(null);
}
return thread.variables[args.VAR] ?? "";
}

threadVariableExists(args, util) {
const thread = util.thread;
thread.variables ??= Object.create(null);
return Object.hasOwn(thread.variables, args.VAR);
if (!thread.variables) {
thread.variables = Object.create(null);
}
return Object.prototype.hasOwnProperty.call(thread.variables, args.VAR);
}

forEachThreadVariable(args, util) {
const thread = util.thread;
thread.variables ??= Object.create(null);
if (!thread.variables) {
thread.variables = Object.create(null);
}
const vars = thread.variables;
util.stackFrame.index ??= 0;
if (!Object.prototype.hasOwnProperty.call(util.stackFrame, "index")) {
util.stackFrame.index = 0;
}
if (util.stackFrame.index < Number(args.NUM)) {
util.stackFrame.index++;
vars[args.VAR] = util.stackFrame.index;
Expand All @@ -256,7 +268,9 @@

listThreadVariables(args, util) {
const thread = util.thread;
thread.variables ??= Object.create(null);
if (!thread.variables) {
thread.variables = Object.create(null);
}
return Object.keys(thread.variables).join(",");
}

Expand All @@ -277,7 +291,10 @@
}

runtimeVariableExists(args) {
return Object.hasOwn(this.runtimeVariables, args.VAR);
return Object.prototype.hasOwnProperty.call(
this.runtimeVariables,
args.VAR
);
}

listRuntimeVariables(args, util) {
Expand All @@ -292,8 +309,9 @@
this.runtimeVariables = Object.create(null);
}
}
// The expose format follows scratch's convention of `ext_${extensionId}`.
// The expose format follows TurboWarp's convention of `ext_${extensionId}`.
// Expose the extension on runtime for others to use.
Scratch.vm.runtime.ext_lmsTempVars2 = new TempVars();
Scratch.extensions.register(Scratch.vm.runtime.ext_lmsTempVars2);
const extension = new TempVars();
Scratch.vm.runtime.ext_lmsTempVars2 = extension;
Scratch.extensions.register(extension);
})(Scratch);

0 comments on commit 0f38017

Please sign in to comment.