feat: add utf8 encoding and decoding
This commit is contained in:
parent
4b1ec671e2
commit
2cfdd8e335
20
src/me/topchetoeu/jscript/lib/EncodingLib.java
Normal file
20
src/me/topchetoeu/jscript/lib/EncodingLib.java
Normal 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);
|
||||
}
|
||||
}
|
@ -119,6 +119,7 @@ public class Internals {
|
||||
|
||||
glob.define(null, "Math", false, wp.getNamespace(MathLib.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, "Object", false, wp.getConstr(ObjectLib.class));
|
||||
|
Loading…
Reference in New Issue
Block a user