unified extend and extendContent to work for any class

This commit is contained in:
DeltaNedas 2020-11-21 13:04:17 +00:00
parent 522a7f4728
commit 09eed078cd
2 changed files with 40 additions and 14 deletions

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const log = function(context, obj){ function log(context, obj){
Vars.mods.scripts.log(context, String(obj)) Vars.mods.scripts.log(context, String(obj))
} }
@ -19,13 +19,26 @@ let modName = "none"
const print = text => log(modName + "/" + scriptName, text); const print = text => log(modName + "/" + scriptName, text);
const extendContent = function(classType, name, params){ // js 'extend(Base, ..., {})' = java 'new Base(...) {}'
return new JavaAdapter(classType, params, name) function extend(/*Base, ..., def*/) {
const Base = arguments[0];
const def = arguments[arguments.length - 1];
// swap order from Base, def, ... to Base, ..., def
const args = [Base, def].concat(Array.from(arguments).splice(1, arguments.length - 2));
// forward constructor arguments to new JavaAdapter
const instance = JavaAdapter.apply(null, args);
// JavaAdapter only overrides functions; set fields too
for (var i in def) {
if (typeof(def[i]) != "function") {
instance[i] = def[i];
}
}
return instance;
} }
const extend = function(classType, params){ // For backwards compatibility, use extend instead
return new JavaAdapter(classType, params) const extendContent = extend;
}
//these are not sctrictly necessary, but are kept for edge cases //these are not sctrictly necessary, but are kept for edge cases
const run = method => new java.lang.Runnable(){run: method} const run = method => new java.lang.Runnable(){run: method}
@ -38,4 +51,4 @@ const prov = method => new Prov(){get: method}
const func = method => new Func(){get: method} const func = method => new Func(){get: method}
const newEffect = (lifetime, renderer) => new Effects.Effect(lifetime, new Effects.EffectRenderer({render: renderer})) const newEffect = (lifetime, renderer) => new Effects.Effect(lifetime, new Effects.EffectRenderer({render: renderer}))
Call = Packages.mindustry.gen.Call const Call = Packages.mindustry.gen.Call

View File

@ -2,7 +2,7 @@
"use strict"; "use strict";
const log = function(context, obj){ function log(context, obj){
Vars.mods.getScripts().log(context, String(obj)) Vars.mods.getScripts().log(context, String(obj))
} }
@ -21,13 +21,26 @@ let modName = "none"
const print = text => log(modName + "/" + scriptName, text); const print = text => log(modName + "/" + scriptName, text);
const extendContent = function(classType, name, params){ // js 'extend(Base, ..., {})' = java 'new Base(...) {}'
return new JavaAdapter(classType, params, name) function extend(/*Base, ..., def*/) {
const Base = arguments[0];
const def = arguments[arguments.length - 1];
// swap order from Base, def, ... to Base, ..., def
const args = [Base, def].concat(Array.from(arguments).splice(1, arguments.length - 2));
// forward constructor arguments to new JavaAdapter
const instance = JavaAdapter.apply(null, args);
// JavaAdapter only overrides functions; set fields too
for (var i in def) {
if (typeof(def[i]) != "function") {
instance[i] = def[i];
}
}
return instance;
} }
const extend = function(classType, params){ // For backwards compatibility, use extend instead
return new JavaAdapter(classType, params) const extendContent = extend;
}
//these are not sctrictly necessary, but are kept for edge cases //these are not sctrictly necessary, but are kept for edge cases
const run = method => new java.lang.Runnable(){run: method} const run = method => new java.lang.Runnable(){run: method}
@ -40,7 +53,7 @@ const prov = method => new Prov(){get: method}
const func = method => new Func(){get: method} const func = method => new Func(){get: method}
const newEffect = (lifetime, renderer) => new Effects.Effect(lifetime, new Effects.EffectRenderer({render: renderer})) const newEffect = (lifetime, renderer) => new Effects.Effect(lifetime, new Effects.EffectRenderer({render: renderer}))
Call = Packages.mindustry.gen.Call const Call = Packages.mindustry.gen.Call
importPackage(Packages.arc) importPackage(Packages.arc)
importPackage(Packages.arc.func) importPackage(Packages.arc.func)