feat: add utf8 encoding and decoding

This commit is contained in:
TopchetoEU 2023-11-25 19:12:56 +02:00
parent 4b1ec671e2
commit 2cfdd8e335
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.values.ArrayValue;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native;
@Native("Encoding")
public class EncodingLib {
@Native public static ArrayValue encode(String value) {
var res = new ArrayValue();
for (var el : value.getBytes()) res.set(null, res.size(), (int)el);
return res;
}
@Native public static String decode(Context ctx, ArrayValue raw) {
var res = new byte[raw.size()];
for (var i = 0; i < raw.size(); i++) res[i] = (byte)Values.toNumber(ctx, raw.get(i));
return new String(res);
}
}

View File

@ -119,6 +119,7 @@ public class Internals {
glob.define(null, "Math", false, wp.getNamespace(MathLib.class)); glob.define(null, "Math", false, wp.getNamespace(MathLib.class));
glob.define(null, "JSON", false, wp.getNamespace(JSONLib.class)); glob.define(null, "JSON", false, wp.getNamespace(JSONLib.class));
glob.define(null, "Encoding", false, wp.getNamespace(EncodingLib.class));
glob.define(null, "Date", false, wp.getConstr(DateLib.class)); glob.define(null, "Date", false, wp.getConstr(DateLib.class));
glob.define(null, "Object", false, wp.getConstr(ObjectLib.class)); glob.define(null, "Object", false, wp.getConstr(ObjectLib.class));