Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c775d7

Browse files
committed
update for Clazz._loadWasm
1 parent 886f7f1 commit 3c775d7

File tree

9 files changed

+111
-27
lines changed

9 files changed

+111
-27
lines changed
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20250310145920
1+
20250313105418
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20250310145920
1+
20250313105418
Binary file not shown.

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
// Google closure compiler cannot handle Clazz.new or Clazz.super
99

10-
// BH 2025.03.06 adds support for JNA+WASM
10+
// BH 2025.03.12 adds support for writable byte[] parameters in WASM
11+
// BH 2025.03.06 adds support for JNA+WASM, automated loading of Java native classes if WASM is available
1112
// BH 2025.02.22 add hashCode$() for Java Integer.TYPE and related types
1213
// BH 2025.01.31 added checks for JavaScript SyntaxError similar to other Error types
1314
// BH 2024.11.23 implementing java.awt.Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval")
@@ -726,6 +727,8 @@ Clazz._loadWasm = function(cls, lib){
726727
return;
727728
cls.wasmLoaded = true;
728729
var libName = lib.getName$(); // "jnainchi" var wasmName = libName + ".wasm";
730+
if (J2S.wasm && J2S.wasm[libname])
731+
return;
729732
var className = cls.getName$();
730733
var classPath = className.substring(0, className.lastIndexOf(".") + 1).replaceAll(".", "/");
731734
var j2sdir = Thread.秘thisThread.getContextClassLoader$().$_$base;
@@ -738,18 +741,57 @@ Clazz._loadWasm = function(cls, lib){
738741
var funcs = [];
739742
J2S.wasm || (J2S.wasm = {});
740743
J2S.wasm[libName] || (J2S.wasm[libName] = {});
741-
if (!J2S[libName])
742744
var getFunc = function(cls, newName, sig, ptypes, retType, fargs, fret) {
743745
System.out.println("Clazz.loadWasm creating J2S.wasm." + libName + "." + newName);
744746
return function(module) {
745-
var f = module.cwrap(newName, retType, ptypes);
747+
var f = [];
748+
f[0] = module.cwrap(newName, retType, ptypes);
746749
//System.out.println(newName + " " + retType + " " + ptypes);
747750
J2S.wasm[libName][newName] = jsClass[newName] = jsClass[sig] = function() {
748751
var rgs = [];
752+
var ba = [];
753+
var pa = [];
754+
var getNewFunc = false;
749755
for (var i = arguments.length; --i >= 0;) {
750-
rgs[i] = (fargs[i] ? fargs[i](arguments[i]) : arguments[i]);
756+
var a = arguments[i];
757+
if (a && a.__NDIM) {
758+
// we have a signature "BA" (byte[]), and the
759+
// developer has passed byte[] rather than the assumed
760+
// String value. So we need to allocate memory for
761+
// the array, fill it, change the wrapping to "number" (for a pointer)
762+
// execute the function, and then retrieve the value.
763+
// Emscripten missed this for some reason, and only reads the array
764+
// but does not re-fill it. I think this is a bug.
765+
ba[i] = a;
766+
pa[i] = module._malloc(a.length);
767+
module.writeArrayToMemory(ba[i], pa[i]);
768+
a = pa[i];
769+
if (ptypes[i] == "string") {
770+
// now we know that a byte[] is being used at runtime instead of a String,
771+
// we need to recreate the wrapped function.
772+
// this will only happen once.
773+
ptypes[i] = "number";
774+
getNewFunc = true;
775+
}
776+
}
777+
rgs[i] = (fargs[i] ? fargs[i](a) : a);
778+
}
779+
if (getNewFunc) {
780+
f[0] = module.cwrap(newName, retType, ptypes);
781+
}
782+
var val = f[0].apply(null, rgs);
783+
if (ba.length) {
784+
for (var i = pa.length; --i >= 0;) {
785+
if (pa[i]) {
786+
// fill original array from pointer
787+
for (var pt = pa[i], a = ba[i], n = a.length, j = 0; j < n; j++) {
788+
a[j] = module.getValue(pt++);
789+
}
790+
module._free(pa[i]);
791+
}
792+
}
793+
751794
}
752-
var val = f.apply(null, rgs);
753795
return (fret ? fret(val, module) : val);
754796
}
755797
}
@@ -854,14 +896,14 @@ Clazz._loadWasm = function(cls, lib){
854896
J2S._wasmPath = libPath;
855897
var src = libPath + libName + ".js";
856898
$.getScript(src, function() {jnainchiModule().then(
857-
function(module){
858-
J2S._module = module;
859-
for (var i = 0; i < funcs.length; i++) {
860-
funcs[i].apply(null, [module]);
861-
}
862-
J2S.inchiWasmLoaded = true;
863-
})
864-
});
899+
function(module){
900+
J2S._module = module;
901+
for (var i = 0; i < funcs.length; i++) {
902+
funcs[i].apply(null, [module]);
903+
}
904+
cls.wasmInitialized = true;
905+
})
906+
});
865907
}
866908

867909
Clazz.newClass = function (prefix, name, clazz, clazzSuper, interfacez, type) {

sources/net.sf.j2s.java.core/srcjs/swingjs2.js

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14143,6 +14143,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
1414314143

1414414144
// Google closure compiler cannot handle Clazz.new or Clazz.super
1414514145

14146+
// BH 2025.03.12 adds support for writable byte[] parameters in WASM
1414614147
// BH 2025.03.06 adds support for JNA+WASM
1414714148
// BH 2025.02.22 add hashCode$() for Java Integer.TYPE and related types
1414814149
// BH 2025.01.31 added checks for JavaScript SyntaxError similar to other Error types
@@ -14862,6 +14863,8 @@ Clazz._loadWasm = function(cls, lib){
1486214863
return;
1486314864
cls.wasmLoaded = true;
1486414865
var libName = lib.getName$(); // "jnainchi" var wasmName = libName + ".wasm";
14866+
if (J2S.wasm && J2S.wasm[libname])
14867+
return;
1486514868
var className = cls.getName$();
1486614869
var classPath = className.substring(0, className.lastIndexOf(".") + 1).replaceAll(".", "/");
1486714870
var j2sdir = Thread.秘thisThread.getContextClassLoader$().$_$base;
@@ -14874,18 +14877,57 @@ Clazz._loadWasm = function(cls, lib){
1487414877
var funcs = [];
1487514878
J2S.wasm || (J2S.wasm = {});
1487614879
J2S.wasm[libName] || (J2S.wasm[libName] = {});
14877-
if (!J2S[libName])
1487814880
var getFunc = function(cls, newName, sig, ptypes, retType, fargs, fret) {
1487914881
System.out.println("Clazz.loadWasm creating J2S.wasm." + libName + "." + newName);
1488014882
return function(module) {
14881-
var f = module.cwrap(newName, retType, ptypes);
14883+
var f = [];
14884+
f[0] = module.cwrap(newName, retType, ptypes);
1488214885
//System.out.println(newName + " " + retType + " " + ptypes);
1488314886
J2S.wasm[libName][newName] = jsClass[newName] = jsClass[sig] = function() {
1488414887
var rgs = [];
14888+
var ba = [];
14889+
var pa = [];
14890+
var getNewFunc = false;
1488514891
for (var i = arguments.length; --i >= 0;) {
14886-
rgs[i] = (fargs[i] ? fargs[i](arguments[i]) : arguments[i]);
14892+
var a = arguments[i];
14893+
if (a && a.__NDIM) {
14894+
// we have a signature "BA" (byte[]), and the
14895+
// developer has passed byte[] rather than the assumed
14896+
// String value. So we need to allocate memory for
14897+
// the array, fill it, change the wrapping to "number" (for a pointer)
14898+
// execute the function, and then retrieve the value.
14899+
// Emscripten missed this for some reason, and only reads the array
14900+
// but does not re-fill it. I think this is a bug.
14901+
ba[i] = a;
14902+
pa[i] = module._malloc(a.length);
14903+
module.writeArrayToMemory(ba[i], pa[i]);
14904+
a = pa[i];
14905+
if (ptypes[i] == "string") {
14906+
// now we know that a byte[] is being used at runtime instead of a String,
14907+
// we need to recreate the wrapped function.
14908+
// this will only happen once.
14909+
ptypes[i] = "number";
14910+
getNewFunc = true;
14911+
}
14912+
}
14913+
rgs[i] = (fargs[i] ? fargs[i](a) : a);
14914+
}
14915+
if (getNewFunc) {
14916+
f[0] = module.cwrap(newName, retType, ptypes);
14917+
}
14918+
var val = f[0].apply(null, rgs);
14919+
if (ba.length) {
14920+
for (var i = pa.length; --i >= 0;) {
14921+
if (pa[i]) {
14922+
// fill original array from pointer
14923+
for (var pt = pa[i], a = ba[i], n = a.length, j = 0; j < n; j++) {
14924+
a[j] = module.getValue(pt++);
14925+
}
14926+
module._free(pa[i]);
14927+
}
14928+
}
14929+
1488714930
}
14888-
var val = f.apply(null, rgs);
1488914931
return (fret ? fret(val, module) : val);
1489014932
}
1489114933
}
@@ -14990,14 +15032,14 @@ Clazz._loadWasm = function(cls, lib){
1499015032
J2S._wasmPath = libPath;
1499115033
var src = libPath + libName + ".js";
1499215034
$.getScript(src, function() {jnainchiModule().then(
14993-
function(module){
14994-
J2S._module = module;
14995-
for (var i = 0; i < funcs.length; i++) {
14996-
funcs[i].apply(null, [module]);
14997-
}
14998-
J2S.inchiWasmLoaded = true;
14999-
})
15000-
});
15035+
function(module){
15036+
J2S._module = module;
15037+
for (var i = 0; i < funcs.length; i++) {
15038+
funcs[i].apply(null, [module]);
15039+
}
15040+
cls.wasmInitialized = true;
15041+
})
15042+
});
1500115043
}
1500215044

1500315045
Clazz.newClass = function (prefix, name, clazz, clazzSuper, interfacez, type) {

0 commit comments

Comments
 (0)