Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Overhaul vm module #86

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
| [tty](https://nodejs.org/api/tty.html) | :heavy_check_mark: |
| [url](https://nodejs.org/api/url.html) | :heavy_check_mark: |
| [util](https://nodejs.org/api/util.html) | :heavy_check_mark: |
| [vm](https://nodejs.org/api/vm.html) | |
| [vm](https://nodejs.org/api/vm.html) | :heavy_check_mark: |
| [zlib](https://nodejs.org/api/zlib.html) | |

## How to use
Expand Down

This file was deleted.

This file was deleted.

76 changes: 76 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/vm/Script.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.scalajs.nodejs.vm

import com.thoughtworks.enableIf

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import scala.scalajs.js.typedarray.{DataView, Uint8Array}
import scala.scalajs.js.|

/**
* Contextify Script
*/
@js.native
@JSImport("vm", "Script")
class Script private[this] () extends js.Object {

def this(code: String, options: ScriptOptions = js.native) = this()
def this(code: String, filename: String) = this()

@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def createCachedData(): io.scalajs.nodejs.buffer.Buffer = js.native

/**
* Runs the compiled code contained by the vm.Script object within the given contextifiedSandbox and returns the
* result. Running code does not have access to local scope.
* @param contextifiedSandbox A contextified object as returned by the vm.createContext() method.
* @param options the optional options
* @example script.runInContext(contextifiedSandbox[, options])
*/
def runInContext(contextifiedSandbox: ScriptContext, options: RunInContextOptions = js.native): js.Any = js.native

/**
* First contextifies the given sandbox, runs the compiled code contained by the vm.Script object within the created
* sandbox, and returns the result. Running code does not have access to local scope.
* @param sandbox An object that will be contextified. If undefined, a new object will be created.
* @param options the optional options
* @example script.runInNewContext([sandbox][, options])
*/
def runInNewContext(sandbox: js.Object, options: RunInNewContextOptions = js.native): js.Any = js.native
def runInNewContext(): js.Any = js.native

/**
* Runs the compiled code contained by the vm.Script within the context of the current global object. Running code
* does not have access to local scope, but does have access to the current global object.
* @param options the optional options
* @example script.runInThisContext([options])
*/
def runInThisContext(options: RunInContextOptions = js.native): Script = js.native

}

class ScriptOptions(var filename: js.UndefOr[String] = js.undefined,
var lineOffset: js.UndefOr[Int] = js.undefined,
var columnOffset: js.UndefOr[Int] = js.undefined,
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
var importModuleDynamically: js.UndefOr[js.Function] = js.undefined)
extends js.Object

class RunInContextOptions(
var displayErrors: js.UndefOr[Boolean] = js.undefined,
var timeout: js.UndefOr[Int] = js.undefined,
var breakOnSigint: js.UndefOr[Boolean] = js.undefined
) extends js.Object

class RunInNewContextOptions(
var displayErrors: js.UndefOr[Boolean] = js.undefined,
var timeout: js.UndefOr[Int] = js.undefined,
var breakOnSigint: js.UndefOr[Boolean] = js.undefined,
var contextName: js.UndefOr[String] = js.undefined,
var contextOrigin: js.UndefOr[String] = js.undefined,
var contextCodeGeneration: js.UndefOr[CodeGeneration] = js.undefined
) extends js.Object

class CodeGeneration(var strings: js.UndefOr[Boolean] = js.undefined, var wasm: js.UndefOr[Boolean] = js.undefined)
extends js.Object

This file was deleted.

92 changes: 65 additions & 27 deletions app/current/src/main/scala/io/scalajs/nodejs/vm/VM.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.scalajs.nodejs.vm

import com.thoughtworks.enableIf

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import scala.scalajs.js.typedarray.{DataView, Uint8Array}
import scala.scalajs.js.|

/**
* The vm module provides APIs for compiling and running code within V8 Virtual Machine contexts.
Expand All @@ -10,9 +14,13 @@ import scala.scalajs.js.annotation.JSImport
@js.native
trait VM extends js.Object {

/////////////////////////////////////////////////////////////////////////////////
// Methods
/////////////////////////////////////////////////////////////////////////////////
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def compileFunction(code: String,
params: js.Array[String],
options: CompileFunctionOptions = js.native): js.Function = js.native

@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def compileFunction(code: String): js.Function = js.native

/**
* If given a sandbox object, the vm.createContext() method will
Expand All @@ -30,22 +38,15 @@ trait VM extends js.Object {
* @example vm.createContext([sandbox])
* @since 0.11.7
*/
def createContext(sandbox: js.Any): ScriptContext = js.native

/**
* Creates a new script
* @param code The JavaScript code to compile.
* @param options the optional options
* @example vm.createScript(code[, options])
*/
def createScript(code: String, options: ScriptOptions = js.native): ContextifyScript = js.native
def createContext(sandbox: js.Object, options: CreateContextOptions = js.native): ScriptContext = js.native
def createContext(): ScriptContext = js.native

/**
* The vm.runInDebugContext() method compiles and executes code inside the V8 debug context.
* @param code The JavaScript code to compile and run.
* @example vm.runInDebugContext(code)
* Returns true if the given sandbox object has been contextified using vm.createContext().
* @param sandbox the sandbox
* @return true if the given sandbox object has been contextified using vm.createContext().
*/
def runInDebugContext(code: String): js.Any = js.native
def isContext(sandbox: js.Object): Boolean = js.native

/**
* Runs the compiled code contained by the vm.Script object within the given contextifiedSandbox and returns the
Expand All @@ -55,8 +56,9 @@ trait VM extends js.Object {
* @param options the optional options
* @example script.runInContext(contextifiedSandbox[, options])
*/
def runInContext(code: String, contextifiedSandbox: ScriptContext, options: ContextOptions = js.native): js.Any =
js.native
def runInContext(code: String,
contextifiedSandbox: ScriptContext,
options: VMRunInContextOptions = js.native): js.Any = js.native

/**
* First contextifies the given sandbox, runs the compiled code contained by the vm.Script object within the created
Expand All @@ -66,7 +68,7 @@ trait VM extends js.Object {
* @param options the optional options
* @example vm.runInNewContext(code[, sandbox][, options])
*/
def runInNewContext(code: String, sandbox: js.Any, options: ContextOptions = js.native): js.Any =
def runInNewContext(code: String, sandbox: js.Any, options: VMRunInNewContextOptions = js.native): js.Any =
js.native

/**
Expand All @@ -76,14 +78,7 @@ trait VM extends js.Object {
* @param options the optional options
* @example script.runInThisContext([options])
*/
def runInThisContext(code: String, options: ContextOptions = js.native): ContextifyScript = js.native

/**
* Returns true if the given sandbox object has been contextified using vm.createContext().
* @param sandbox the sandbox
* @return true if the given sandbox object has been contextified using vm.createContext().
*/
def isContext(sandbox: js.Any): Boolean = js.native
def runInThisContext(code: String, options: VMRunInContextOptions = js.native): Script = js.native

}

Expand All @@ -93,3 +88,46 @@ trait VM extends js.Object {
@js.native
@JSImport("vm", JSImport.Namespace)
object VM extends VM

class CompileFunctionOptions(
var filename: js.UndefOr[String] = js.undefined,
var lineOffset: js.UndefOr[Int] = js.undefined,
var columnOffset: js.UndefOr[Int] = js.undefined,
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
@deprecated("Use script.createCachedData", "Node.js v10")
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
var parsingContext: js.UndefOr[js.Object] = js.undefined,
var contextExtensions: js.UndefOr[js.Array[js.Object]] = js.undefined
) extends js.Object

class CreateContextOptions(
var name: js.UndefOr[String] = js.undefined,
var origin: js.UndefOr[String] = js.undefined,
var codeGeneration: js.UndefOr[CodeGeneration] = js.undefined
) extends js.Object

class VMRunInNewContextOptions(
var filename: js.UndefOr[String] = js.undefined,
var lineOffset: js.UndefOr[Int] = js.undefined,
var columnOffset: js.UndefOr[Int] = js.undefined,
var displayErrors: js.UndefOr[Boolean] = js.undefined,
var timeout: js.UndefOr[Int] = js.undefined,
var breakOnSigint: js.UndefOr[Boolean] = js.undefined,
var contextName: js.UndefOr[String] = js.undefined,
var contextOrigin: js.UndefOr[String] = js.undefined,
var contextCodeGeneration: js.UndefOr[CodeGeneration] = js.undefined,
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
var importModuleDynamically: js.UndefOr[js.Function] = js.undefined
) extends js.Object

class VMRunInContextOptions(var filename: js.UndefOr[String] = js.undefined,
var lineOffset: js.UndefOr[Int] = js.undefined,
var columnOffset: js.UndefOr[Int] = js.undefined,
var displayErrors: js.UndefOr[Boolean] = js.undefined,
var timeout: js.UndefOr[Int] = js.undefined,
var breakOnSigint: js.UndefOr[Boolean] = js.undefined,
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
var importModuleDynamically: js.UndefOr[js.Function] = js.undefined)
extends js.Object
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class VMTest extends FunSpec {
ExpectedData(animal = "cat", count = 12, name = "kitty12")
)

val script = VM.createScript("""count += 1; name = "kitty" + count""")
val script = new Script("""count += 1; name = "kitty" + count""")
val context = VM.createContext(sandbox)
expectedSet foreach {
case ExpectedData(animal, count, name) =>
Expand Down