move json encode/decode to Go
This commit is contained in:
parent
a6420423a9
commit
55e18c5f30
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
"Moonshark/core/utils/logger"
|
"Moonshark/core/utils/logger"
|
||||||
@ -92,7 +93,13 @@ func (s *Sandbox) registerCoreFunctions(state *luajit.State) error {
|
|||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@ -264,3 +271,43 @@ func extractCookie(state *luajit.State, response *Response) {
|
|||||||
|
|
||||||
response.Cookies = append(response.Cookies, cookie)
|
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
|
||||||
|
}
|
||||||
|
@ -382,40 +382,18 @@ local csrf = {
|
|||||||
|
|
||||||
-- Utility module implementation
|
-- Utility module implementation
|
||||||
local util = {
|
local util = {
|
||||||
-- Generate a token (wrapper around __generate_token)
|
|
||||||
generate_token = function(length)
|
generate_token = function(length)
|
||||||
return __generate_token(length or 32)
|
return __generate_token(length or 32)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
-- Simple JSON stringify (for when you just need a quick string)
|
json_encode = function(string)
|
||||||
json_encode = function(value)
|
return __json_marshal(string or "{}")
|
||||||
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) .. '"'
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
json_decode = function(string)
|
||||||
|
return __json_unmarshal(string or "{}")
|
||||||
|
end,
|
||||||
|
|
||||||
-- Deep copy of tables
|
-- Deep copy of tables
|
||||||
deep_copy = function(obj)
|
deep_copy = function(obj)
|
||||||
if type(obj) ~= 'table' then return obj end
|
if type(obj) ~= 'table' then return obj end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user