Compare commits
2 Commits
0f57e8ffcb
...
803ad02772
| Author | SHA1 | Date | |
|---|---|---|---|
| 803ad02772 | |||
| bd0e0d8fec |
@ -39,7 +39,7 @@ type Message struct {
|
|||||||
Type MsgType `json:"type"`
|
Type MsgType `json:"type"`
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Params Vals `json:"params"`
|
Args Vals `json:"args"`
|
||||||
Result Vals `json:"result"`
|
Result Vals `json:"result"`
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
@ -118,14 +118,25 @@ func (ipc *ipcCommon) handleCall(msg Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
argsCount := method.Type().NumIn()
|
argsCount := method.Type().NumIn()
|
||||||
if len(msg.Params) != argsCount {
|
if len(msg.Args) != argsCount {
|
||||||
ipc.sendResponse(msg.Id, nil, fmt.Errorf("args count mismatch: expected %d, got %d", argsCount, len(msg.Params)))
|
ipc.sendResponse(msg.Id, nil, fmt.Errorf("args count mismatch: expected %d, got %d", argsCount, len(msg.Args)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var args []reflect.Value
|
var args []reflect.Value
|
||||||
for _, param := range msg.Params {
|
for i, arg := range msg.Args {
|
||||||
args = append(args, reflect.ValueOf(param))
|
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)
|
results := method.Call(args)
|
||||||
@ -221,7 +232,7 @@ func (ipc *ipcCommon) Call(method string, params ...any) (Vals, error) {
|
|||||||
Type: MsgCall,
|
Type: MsgCall,
|
||||||
Id: id,
|
Id: id,
|
||||||
Method: method,
|
Method: method,
|
||||||
Params: params,
|
Args: params,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ipc.sendMsg(msg); err != nil {
|
if err := ipc.sendMsg(msg); err != nil {
|
||||||
|
|||||||
@ -20,7 +20,7 @@ interface CallMessage {
|
|||||||
type: MsgType.Call,
|
type: MsgType.Call,
|
||||||
id: number,
|
id: number,
|
||||||
method: string;
|
method: string;
|
||||||
params: Vals;
|
args: Vals;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ResponseMessage {
|
interface ResponseMessage {
|
||||||
@ -129,18 +129,18 @@ abstract class IPCCommon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const argsCount = method.length;
|
const argsCount = method.length;
|
||||||
if (msg.params.length !== argsCount) {
|
if (msg.args.length !== argsCount) {
|
||||||
this.sendMsg({
|
this.sendMsg({
|
||||||
type: MsgType.Response,
|
type: MsgType.Response,
|
||||||
id: msg.id,
|
id: msg.id,
|
||||||
error: `argument count mismatch: expected ${ argsCount }, got ${ msg.params.length }`
|
error: `argument count mismatch: expected ${ argsCount }, got ${ msg.args.length }`
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.processingCalls++;
|
this.processingCalls++;
|
||||||
let result = method.apply(endpoint, msg.params);
|
let result = method.apply(endpoint, msg.args);
|
||||||
if (result instanceof Promise) {
|
if (result instanceof Promise) {
|
||||||
result = await result;
|
result = await result;
|
||||||
}
|
}
|
||||||
@ -192,7 +192,7 @@ abstract class IPCCommon {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
this.sendMsg({type: MsgType.Call, id, method, params});
|
this.sendMsg({type: MsgType.Call, id, method, args: params});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
delete this.pendingCalls[id];
|
delete this.pendingCalls[id];
|
||||||
reject(new Error(`send call: ${ e }`));
|
reject(new Error(`send call: ${ e }`));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user