class Duktape::Runtime
- Duktape::Runtime
- Reference
- Object
Overview
A Runtime is a simplified mechanism for evaluating javascript code without directly using the low-level Duktape API calls.
Instances of the Runtime class may evaluate code using an interface
inspired by ExecJS. The method calls
within this class all return the last evaluated value (with the exception
of #exec
).
rt = Duktape::Runtime.new
rt.eval("Math.PI") # => 3.14159
The Runtime class also allows for javascript initialization code:
rt = Duktape::Runtime.new do |ctx|
ctx.eval! <<-JS
function add_one(num){ return num + 1; }
JS
end
rt.eval("add_one", 42) # => 43.0
The Runtime class is not loaded by default and must be required before using it:
require "duktape/runtime"
Also note that the Runtime class includes the base Duktape code for you and may be used in a standalone manner.
Defined in:
duktape/runtime.crConstructors
- .new(timeout : Int32 | Int64)
- .new(context : Duktape::Context)
- .new
- .new(timeout : Int32 | Int64, &)
- .new(context : Duktape::Context, &)
- .new(&)
Instance Method Summary
-
#call(prop : String, *args)
Call the named property with the supplied arguments, returning the value of the called property.
-
#call(props : Array(String), *args)
Call the nested property that is supplied via an array of strings with the supplied arguments.
-
#eval(source : String)
Evaluate the supplied source code on the underlying javascript context and return the last value:
-
#exec(source : String)
Execute the supplied source code on the underyling javascript context without returning any value.
Constructor Detail
Instance Method Detail
Call the named property with the supplied arguments, returning the value of the called property.
This call will raise a Duktape::Error
if the
last evaluated expression threw an error.
The property string can include parent objects:
rt = Duktape::Runtime.new
rt.call("JSON.stringify", 123) # => "123"
Call the nested property that is supplied via an array of strings with the supplied arguments.
This call will raise a Duktape::Error
if the
last evaluated expression threw an error.
rt = Duktape::Runtime.new
rt.call(["Math", "PI"]) # => 3.14159
Evaluate the supplied source code on the underlying javascript context and return the last value:
rt = Duktape::Runtime.new
rt.eval("1 + 1") => 2.0
Execute the supplied source code on the underyling javascript context without returning any value.
rt = Duktape::Runtime.new
rt.exec("1 + 1") # => nil