From 55e18c5f30c0f830bd9ff050c4c65967e8d5c4ce Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Fri, 2 May 2025 07:20:30 -0500 Subject: [PATCH] move json encode/decode to Go --- core/runner/sandbox.go | 49 ++++++++++++++++++++++++++++++++++++++++- core/runner/sandbox.lua | 34 +++++----------------------- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/core/runner/sandbox.go b/core/runner/sandbox.go index b5cdfdf..d3bdc41 100644 --- a/core/runner/sandbox.go +++ b/core/runner/sandbox.go @@ -4,6 +4,7 @@ import ( "fmt" "sync" + "github.com/goccy/go-json" "github.com/valyala/fasthttp" "Moonshark/core/utils/logger" @@ -92,7 +93,13 @@ func (s *Sandbox) registerCoreFunctions(state *luajit.State) error { return err } - // Additional registrations can be added here + if err := state.RegisterGoFunction("__json_marshal", jsonMarshal); err != nil { + return err + } + + if err := state.RegisterGoFunction("__json_unmarshal", jsonUnmarshal); err != nil { + return err + } return nil } @@ -264,3 +271,43 @@ func extractCookie(state *luajit.State, response *Response) { response.Cookies = append(response.Cookies, cookie) } + +// jsonMarshal converts a Lua value to a JSON string +func jsonMarshal(state *luajit.State) int { + value, err := state.ToValue(1) + if err != nil { + state.PushString(fmt.Sprintf("json marshal error: %v", err)) + return 1 + } + + bytes, err := json.Marshal(value) + if err != nil { + state.PushString(fmt.Sprintf("json marshal error: %v", err)) + return 1 + } + + state.PushString(string(bytes)) + return 1 +} + +// jsonUnmarshal converts a JSON string to a Lua value +func jsonUnmarshal(state *luajit.State) int { + if !state.IsString(1) { + state.PushString("json unmarshal error: expected string") + return 1 + } + jsonStr := state.ToString(1) + + var value any + err := json.Unmarshal([]byte(jsonStr), &value) + if err != nil { + state.PushString(fmt.Sprintf("json unmarshal error: %v", err)) + return 1 + } + + if err := state.PushValue(value); err != nil { + state.PushString(fmt.Sprintf("json unmarshal error: %v", err)) + return 1 + } + return 1 +} diff --git a/core/runner/sandbox.lua b/core/runner/sandbox.lua index 368b857..d9e81b8 100644 --- a/core/runner/sandbox.lua +++ b/core/runner/sandbox.lua @@ -382,40 +382,18 @@ local csrf = { -- Utility module implementation local util = { - -- Generate a token (wrapper around __generate_token) generate_token = function(length) return __generate_token(length or 32) end, - -- Simple JSON stringify (for when you just need a quick string) - json_encode = function(value) - if type(value) == "table" then - local json = "{" - local sep = "" - for k, v in pairs(value) do - json = json .. sep - if type(k) == "number" then - -- Array-like - json = json .. util.json_encode(v) - else - -- Object-like - json = json .. '"' .. k .. '":' .. util.json_encode(v) - end - sep = "," - end - return json .. "}" - elseif type(value) == "string" then - return '"' .. value:gsub('"', '\\"'):gsub('\n', '\\n') .. '"' - elseif type(value) == "number" then - return tostring(value) - elseif type(value) == "boolean" then - return value and "true" or "false" - elseif value == nil then - return "null" - end - return '"' .. tostring(value) .. '"' + json_encode = function(string) + return __json_marshal(string or "{}") end, + json_decode = function(string) + return __json_unmarshal(string or "{}") + end, + -- Deep copy of tables deep_copy = function(obj) if type(obj) ~= 'table' then return obj end