Skip to content

Creating a hosting context

fholm edited this page Apr 6, 2011 · 2 revisions

F#

Creating a hosting context for IronJS is very simple, first you open the namespaces and modules you'll need:

open IronJS
module IronJS = IronJS.Hosting.FSharp

Then call createContext on the IronJS.Hosting.FSharp (aliased as IronJS):

let ctx = IronJS.createContext()

To execute JavaScript source code you have access to the following methods on the Hosting.FSharp module

  • execute : string -> IronJS.Hosting.FSharp.T -> obj
  • executeAs<'a> : string -> IronJS.Hosting.FSharp.T -> 'a
  • executeFile : string -> IronJS.Hosting.FSharp.T -> obj
  • executeFileAs<'a> : string -> IronJS.Hosting.FSharp.T -> 'a

For working with global values you have access to the following fucntions:

  • setGlobal: string -> obj -> IronJS.Hosting.FSharp.T -> unit
  • getGlobal: string -> IronJS.Hosting.FSharp.T -> IronJS.BoxedValue
  • getGlobalAs<'a>: string -> IronJS.Hosting.FSharp.T -> 'a

You can also access the IronJS environment object and the globals object with the following functions

  • env: -> IronJS.Hosting.FSharp.T -> IronJS.Environment
  • globals: -> IronJS.Hosting.FSharp.T -> IronJS.CommonObject

C#

The process in C# works almost the same as in F#, but the API is more C#-like, you create an instance of the IronJS.Hosting.CSharp.Context object:

var ctx = new IronJS.Hosting.CSharp.Context();

You then have access to the following methods and properties on the context object to run JavaScript code, work with globals and modify the environment:

  • Execute(string)
  • ExecuteAs<T>(string)
  • ExecuteFile(string)
  • ExecuteFileAs<T>(string)
  • SetGlobal(string, obj)
  • GetGlobal(string)
  • GetGlobalAs<T>(string)
  • Environment
  • Globals