New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple functions in my Javascript file #124
Comments
Hi @fallais, Assuming that you have run the code that defines the exclude, _ := goja.AssertFunction(vm.Get("Exclude"))
returnRaw, err := exclude(nil, vm.ToValue(string(incidentJSON))
// note, res will be a goja.Value, if you need a native Go value you need to export it:
v := returnRaw.Export() Hope this helps. |
Thanks a lot ! Here is the code, it should find it.. // Exclude returns true if the incident is excluded. Otherwise it must return false.
var Exclude = function(incident){
...
} |
I do not set the script file anywhere.. that is the issue I guess.. ^^ // Create the VM
vm := goja.New()
// Set the variables
vm.Set("customer", customer)
vm.Set("incident", incidentJSON)
// Run
excludeFunc, ok := goja.AssertFunction(vm.Get("Exclude"))
if !ok {
return false, fmt.Errorf("Error while asserting the function [Exclude]: %s", err)
}
returnRaw, err := excludeFunc(nil, vm.ToValue(string(incidentJSON)))
if err != nil {
return false, fmt.Errorf("Error while executing the function [Exclude]: %s", err)
}
excluded := returnRaw.ToBoolean() PS : do I really need to marshall into JSON ? Does the lib do it automatically when passing struct into the VM ? |
I can't see you are running anything that could define the |
Yes, I realized it... But I do not understand the logic here. I have to get a So, if I understand correctly, I ned to run it one first time for "nothing" ? |
You don't run the function, you run the code that defines the function. |
Something like this ? // Marshal incident as JSON object
incidentJSON, err := json.Marshal(incident)
if err != nil {
return false, fmt.Errorf("Error while marshalling the incident : %s", err)
}
// Create the VM
vm := goja.New()
// Set the variables
vm.Set("customer", customer)
vm.Set("incident", incidentJSON)
// Run the script
_, err = vm.RunString(norm.script)
if err != nil {
return false, fmt.Errorf("Error while running the JS code: %s", err)
}
// Run
excludeFunc, ok := goja.AssertFunction(vm.Get("Exclude"))
if !ok {
return false, fmt.Errorf("Error while asserting the function [Exclude]: %s", err)
}
returnRaw, err := excludeFunc(nil, vm.ToValue(string(incidentJSON)))
if err != nil {
return false, fmt.Errorf("Error while executing the function [Exclude]: %s", err)
}
excluded := returnRaw.ToBoolean() |
Something like this: vm := goja.New()
_, err = vm.RunString("function Exclude(incident) { ... your code here ... }")
if err != nil {
return false, fmt.Errorf("Error while running the JS code: %s", err)
}
// Run
excludeFunc, ok := goja.AssertFunction(vm.Get("Exclude"))
if !ok {
return false, fmt.Errorf("Error while asserting the function [Exclude]: %s", err)
}
returnRaw, err := excludeFunc(nil, vm.ToValue(string(incidentJSON)))
if err != nil {
return false, fmt.Errorf("Error while executing the function [Exclude]: %s", err)
}
excluded := returnRaw.ToBoolean() I'm not sure about the role of the As for serialising into JSON, you can pass a Go variable directly (like |
Perfect thanks :) |
Well, I have errors with the But the |
If I use |
I can't really help you without seeing a full test case, sorry. |
Hello, I come from Otto world and I want to migrate to this library because of very bad performance issues. I am using it for a normalisation pipeline.
I used to put 4 or 5 functions into a JS file, and then call one of them into my Golang code.
Example (here I call the function Exlcude) :
How can I do this with your lib please ? Thanks a lot ! :)
The text was updated successfully, but these errors were encountered: