On this page:
Runtime  Lib.create
5.1.1 The Pyret Runtime
5.1.1.1 Running Pyret Programs
Runtime.run  Standalone
5.1.1.2 Creating Values
Runtime.make  Number
Runtime.make  Number  From  String
Runtime.make  String
Runtime.pyret  True
Runtime.pyret  False
Runtime.make  Array
Runtime.make  Object
Runtime.make  Function
Runtime.make  Method  N
5.1.1.3 Interacting with Objects
Runtime.get  Field
Runtime.get  Colon  Field
Runtime.get  Fields
Runtime.has  Field
5.1.1.4 Assertions
Runtime.check  Arity
Runtime.check  Number
Runtime.check  String
Runtime.check  Boolean
Runtime.check  Object
Runtime.check  Function
Runtime.check  Method
Runtime.check  Array
Runtime.check  Pyret  Val
5.1.1.5 Equality
Runtime.combine  Equality
5.1.1.6 FFI Helpers
Runtime.ffi

5.1 Runtime API

Pyret exposes most of its internal operations on the runtime object; most JavaScript code that interacts with Pyret will need to know about the runtime.

The library that defines runtimes is in src/js/base/runtime-anf.js, and it is configured to be importable with js/runtime-anf via RequireJS:

define(["js/runtime-anf"], function(runtimeLib) {

  // use runtimeLib

});

RuntimeLib.create(options) → Runtime

Create a new Pyret runtime. The RuntimeLib value itself only exports this interface, and most other useful functions are referenced from the runtime objecs it creates. The options are:

options :: {

  initialGas: Number,

  stdout: String → Undefined,

  stderr: String → Undefined

}

The size of the Pyret stack is constrained to initialGas frames; most applications have little need to set this.

For applications that need control over printing, they can set stdout and stderr to get called whenever Pyret would print strings (e.g. via print). This interface may change to accept all Pyret values at some point, to allow for richer rendering interfaces.

5.1.1 The Pyret Runtime

The return value of RuntimeLib.create is a runtime object with many useful methods for programmatically interacting with Pyret.

5.1.1.1 Running Pyret Programs

!→ means this function is not stack safe
Runtime.runStandalone(
modules: JSDict<URI, StaticModules>
dependencies: JSDict<URI, JSDict<String, URI>>
toLoad: JSArray<URI>
postLoadHooks: JSDict<URI, (PyretModuleResult → Undefined)>
)
!→ PyretModuleResult

Uses toLoad—the list of URIs—to evaluate modules in order. The modules are found in the modules dictionary, and the dependencies dictionary describes which modules they depend on. This structure is described in Complete Programs.

A more important feature of Runtime.runStandalone is the ability to register postLoadHooks. This dictionary, keyed on URI, contains callbacks which are invoked on completion of the corresponding module, and passed the module’s result. The most obvious candidate for a postLoadHook is the main module, or the last one in the toLoad list; this is where test results can be fetched and printed and errors reported, based on the returned [REF PyretModuleResult].

In addition, Pyret’s default standalones register several postLoadHooks. For example, after the ffi library is loaded, a number of new fields are added to runtime to manipulate lists. Other uses include:

While evaluating the modules, the runtime caches the results for each module that completed successfully, in the runtime.modules dictionary. This can be accessed later to quickly get the exported values of a module without re-running it.

5.1.1.2 Creating Values

Runtime.makeNumber(JSNumber) → PyretNumber

Runtime.makeNumberFromString(JSString) → PyretNumber

Parses the string and creates a representation of the number that avoids float overflows and can represent very large and very small rationals exactly.

Runtime.makeString(JSString) → PyretString

The representation of Pyret strings is JS strings, though this may change to accommodate better Unicode support in the future.

Runtime.pyretTrue :: PyretBoolean

Runtime.pyretFalse :: PyretBoolean

The runtime values for true and false in Pyret. Representation is JavaScript true and false.

Runtime.makeArray(JSArray) → PyretRawArray

Creates a Pyret RawArray with the given elements. Currently the identity function: Pyret raw arrays are JavaScript arrays.

Runtime.makeObject(JSObj) → PyretObject

Creates a Pyret object with the fields in the input object. The representation of an object is not one-to-one with JS objects.

The fields of a Pyret object go in the dict field of the JS object, and the JS object has an additional field called brands, which hold information about an object’s type information (if it has any). StringDicts, for example, are branded objects.

Runtime.makeFunction(JSFunction) → PyretFunction

Returns a Pyret function backed by the provided JS function. The Pyret function will not do any arity checks on behalf of the JS function, so any arity checks need to be done explicitly (see Runtime.checkArity). The function can be accessed directly via the app field of the Pyret function, but read the section on Runtime.safeCall in order to suitably protect calls to Pyret functions.

Runtime.makeMethodN(JSFunction) → PyretMethod

5.1.1.3 Interacting with Objects

Runtime.getField(PyretObject, JSString) → PyretValue

Gets the field with the given name from the object. If the field is a method, it is automatically curried over the object, as with dot.

Runtime.getColonField(PyretObject, JSString) → PyretValue

Gets the field with the given name from the object. If the field is a method, no additional work is performed.

Runtime.getFields(PyretObject) → JSArray<String>

Returns all the field names of the given object.

Runtime.hasField(PyretObject, JSString) → JSBoolean

Checks if the given object has the named field.

5.1.1.4 Assertions
Runtime.checkArity(
JSNumber
Arguments
JSString
)
→ Undefined

Checks that the given argument list has the given arity. Throws an exception if they don’t match.

There are a number of checking functions that check that a given argument is of a particular type, and throw an exception if not:

Runtime.checkNumber(Any) → Undefined

Runtime.checkString(Any) → Undefined

Runtime.checkBoolean(Any) → Undefined

Runtime.checkObject(Any) → Undefined

Runtime.checkFunction(Any) → Undefined

Runtime.checkMethod(Any) → Undefined

Runtime.checkArray(Any) → Undefined

Runtime.checkPyretVal(Any) → Undefined

5.1.1.5 Equality

Runtime.combineEquality(EqualityResult, EqualityResult) → EqualityResult

Takes two EqualityResults and combines them. Any value paired with NotEqual produces NotEqual, any combination of Equal and Unknown produces Unknown, and two Equal values produce Equal.

5.1.1.6 FFI Helpers

Runtime.ffi :: FFIHelpers

The Pyret runtime instantiates an FFIHelpers object and stores in in the ffi field.