class Duktape::Runtime

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.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(timeout : Int32 | Int64) #

[View source]
def self.new(context : Duktape::Context) #

[View source]
def self.new #

[View source]
def self.new(timeout : Int32 | Int64, &) #

[View source]
def self.new(context : Duktape::Context, &) #

[View source]
def self.new(&) #

[View source]

Instance Method Detail

def call(prop : String, *args) #

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"

[View source]
def call(props : Array(String), *args) #

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

[View source]
def eval(source : String) #

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

[View source]
def exec(source : String) #

Execute the supplied source code on the underyling javascript context without returning any value.

rt = Duktape::Runtime.new
rt.exec("1 + 1") # => nil

[View source]