Compare commits

..

2 Commits

Author SHA1 Message Date
803ad02772
convert float64 to int 2025-11-15 17:54:30 +03:00
bd0e0d8fec
rename params to args 2025-11-15 17:40:33 +03:00
2 changed files with 22 additions and 11 deletions

View File

@ -39,7 +39,7 @@ type Message struct {
Type MsgType `json:"type"`
Id int64 `json:"id"`
Method string `json:"method"`
Params Vals `json:"params"`
Args Vals `json:"args"`
Result Vals `json:"result"`
Error string `json:"error"`
}
@ -118,14 +118,25 @@ func (ipc *ipcCommon) handleCall(msg Message) {
}
argsCount := method.Type().NumIn()
if len(msg.Params) != argsCount {
ipc.sendResponse(msg.Id, nil, fmt.Errorf("args count mismatch: expected %d, got %d", argsCount, len(msg.Params)))
if len(msg.Args) != argsCount {
ipc.sendResponse(msg.Id, nil, fmt.Errorf("args count mismatch: expected %d, got %d", argsCount, len(msg.Args)))
return
}
var args []reflect.Value
for _, param := range msg.Params {
args = append(args, reflect.ValueOf(param))
for i, arg := range msg.Args {
paramType := method.Type().In(i)
argType := reflect.TypeOf(arg)
// JSON decodes any number to float64. If we need int, we should check and convert
if paramType.Kind() == reflect.Int && argType.Kind() == reflect.Float64 {
floatArg := arg.(float64)
if float64(int64(floatArg)) == floatArg && !paramType.OverflowInt(int64(floatArg)) {
arg = arg.(int)
}
}
args = append(args, reflect.ValueOf(paramType))
}
results := method.Call(args)
@ -221,7 +232,7 @@ func (ipc *ipcCommon) Call(method string, params ...any) (Vals, error) {
Type: MsgCall,
Id: id,
Method: method,
Params: params,
Args: params,
}
if err := ipc.sendMsg(msg); err != nil {

View File

@ -20,7 +20,7 @@ interface CallMessage {
type: MsgType.Call,
id: number,
method: string;
params: Vals;
args: Vals;
}
interface ResponseMessage {
@ -129,18 +129,18 @@ abstract class IPCCommon {
}
const argsCount = method.length;
if (msg.params.length !== argsCount) {
if (msg.args.length !== argsCount) {
this.sendMsg({
type: MsgType.Response,
id: msg.id,
error: `argument count mismatch: expected ${ argsCount }, got ${ msg.params.length }`
error: `argument count mismatch: expected ${ argsCount }, got ${ msg.args.length }`
});
return;
}
try {
this.processingCalls++;
let result = method.apply(endpoint, msg.params);
let result = method.apply(endpoint, msg.args);
if (result instanceof Promise) {
result = await result;
}
@ -192,7 +192,7 @@ abstract class IPCCommon {
}
};
try {
this.sendMsg({type: MsgType.Call, id, method, params});
this.sendMsg({type: MsgType.Call, id, method, args: params});
} catch (e) {
delete this.pendingCalls[id];
reject(new Error(`send call: ${ e }`));