[Lua] Adds some useful encoding APIs for Lua (base64 encoding and decoding and such).
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { LuaBuiltinFunction, LuaTable } from "../runtime.ts";
|
||||
import { base64Decode, base64Encode } from "../../crypto.ts";
|
||||
|
||||
export const encodingApi = new LuaTable({
|
||||
base64Encode: new LuaBuiltinFunction(
|
||||
(_sf, s: string | Uint8Array): string => {
|
||||
return base64Encode(s);
|
||||
},
|
||||
),
|
||||
base64Decode: new LuaBuiltinFunction((_sf, s: string): Uint8Array => {
|
||||
return base64Decode(s);
|
||||
}),
|
||||
utf8Encode: new LuaBuiltinFunction((_sf, s: string): Uint8Array => {
|
||||
return new TextEncoder().encode(s);
|
||||
}),
|
||||
utf8Decode: new LuaBuiltinFunction((_sf, data: Uint8Array): string => {
|
||||
return new TextDecoder().decode(data);
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
local function assertEqual(a, b)
|
||||
if a ~= b then
|
||||
error("Assertion failed: " .. a .. " is not equal to " .. b)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
assertEqual(encoding.utf8Decode(encoding.utf8Encode("test")), "test")
|
||||
assertEqual(encoding.utf8Decode(encoding.base64Decode(encoding.base64Encode("test"))), "test")
|
||||
Reference in New Issue
Block a user