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

Commit 565e692

Browse files
authored
Merge pull request #86 from exoego/vm
Overhaul vm module
2 parents 12e46ef + fa92fc7 commit 565e692

File tree

7 files changed

+143
-145
lines changed

7 files changed

+143
-145
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
4646
| [tty](https://nodejs.org/api/tty.html) | :heavy_check_mark: |
4747
| [url](https://nodejs.org/api/url.html) | :heavy_check_mark: |
4848
| [util](https://nodejs.org/api/util.html) | :heavy_check_mark: |
49-
| [vm](https://nodejs.org/api/vm.html) | |
49+
| [vm](https://nodejs.org/api/vm.html) | :heavy_check_mark: |
5050
| [zlib](https://nodejs.org/api/zlib.html) | |
5151

5252
## How to use

app/current/src/main/scala/io/scalajs/nodejs/vm/ContextOptions.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

app/current/src/main/scala/io/scalajs/nodejs/vm/ContextifyScript.scala

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.scalajs.nodejs.vm
2+
3+
import com.thoughtworks.enableIf
4+
5+
import scala.scalajs.js
6+
import scala.scalajs.js.annotation.JSImport
7+
import scala.scalajs.js.typedarray.{DataView, Uint8Array}
8+
import scala.scalajs.js.|
9+
10+
/**
11+
* Contextify Script
12+
*/
13+
@js.native
14+
@JSImport("vm", "Script")
15+
class Script private[this] () extends js.Object {
16+
17+
def this(code: String, options: ScriptOptions = js.native) = this()
18+
def this(code: String, filename: String) = this()
19+
20+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
21+
def createCachedData(): io.scalajs.nodejs.buffer.Buffer = js.native
22+
23+
/**
24+
* Runs the compiled code contained by the vm.Script object within the given contextifiedSandbox and returns the
25+
* result. Running code does not have access to local scope.
26+
* @param contextifiedSandbox A contextified object as returned by the vm.createContext() method.
27+
* @param options the optional options
28+
* @example script.runInContext(contextifiedSandbox[, options])
29+
*/
30+
def runInContext(contextifiedSandbox: ScriptContext, options: RunInContextOptions = js.native): js.Any = js.native
31+
32+
/**
33+
* First contextifies the given sandbox, runs the compiled code contained by the vm.Script object within the created
34+
* sandbox, and returns the result. Running code does not have access to local scope.
35+
* @param sandbox An object that will be contextified. If undefined, a new object will be created.
36+
* @param options the optional options
37+
* @example script.runInNewContext([sandbox][, options])
38+
*/
39+
def runInNewContext(sandbox: js.Object, options: RunInNewContextOptions = js.native): js.Any = js.native
40+
def runInNewContext(): js.Any = js.native
41+
42+
/**
43+
* Runs the compiled code contained by the vm.Script within the context of the current global object. Running code
44+
* does not have access to local scope, but does have access to the current global object.
45+
* @param options the optional options
46+
* @example script.runInThisContext([options])
47+
*/
48+
def runInThisContext(options: RunInContextOptions = js.native): Script = js.native
49+
50+
}
51+
52+
class ScriptOptions(var filename: js.UndefOr[String] = js.undefined,
53+
var lineOffset: js.UndefOr[Int] = js.undefined,
54+
var columnOffset: js.UndefOr[Int] = js.undefined,
55+
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
56+
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
57+
var importModuleDynamically: js.UndefOr[js.Function] = js.undefined)
58+
extends js.Object
59+
60+
class RunInContextOptions(
61+
var displayErrors: js.UndefOr[Boolean] = js.undefined,
62+
var timeout: js.UndefOr[Int] = js.undefined,
63+
var breakOnSigint: js.UndefOr[Boolean] = js.undefined
64+
) extends js.Object
65+
66+
class RunInNewContextOptions(
67+
var displayErrors: js.UndefOr[Boolean] = js.undefined,
68+
var timeout: js.UndefOr[Int] = js.undefined,
69+
var breakOnSigint: js.UndefOr[Boolean] = js.undefined,
70+
var contextName: js.UndefOr[String] = js.undefined,
71+
var contextOrigin: js.UndefOr[String] = js.undefined,
72+
var contextCodeGeneration: js.UndefOr[CodeGeneration] = js.undefined
73+
) extends js.Object
74+
75+
class CodeGeneration(var strings: js.UndefOr[Boolean] = js.undefined, var wasm: js.UndefOr[Boolean] = js.undefined)
76+
extends js.Object

app/current/src/main/scala/io/scalajs/nodejs/vm/ScriptOptions.scala

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package io.scalajs.nodejs.vm
22

3+
import com.thoughtworks.enableIf
4+
35
import scala.scalajs.js
46
import scala.scalajs.js.annotation.JSImport
7+
import scala.scalajs.js.typedarray.{DataView, Uint8Array}
8+
import scala.scalajs.js.|
59

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

13-
/////////////////////////////////////////////////////////////////////////////////
14-
// Methods
15-
/////////////////////////////////////////////////////////////////////////////////
17+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
18+
def compileFunction(code: String,
19+
params: js.Array[String],
20+
options: CompileFunctionOptions = js.native): js.Function = js.native
21+
22+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
23+
def compileFunction(code: String): js.Function = js.native
1624

1725
/**
1826
* If given a sandbox object, the vm.createContext() method will
@@ -30,22 +38,15 @@ trait VM extends js.Object {
3038
* @example vm.createContext([sandbox])
3139
* @since 0.11.7
3240
*/
33-
def createContext(sandbox: js.Any): ScriptContext = js.native
34-
35-
/**
36-
* Creates a new script
37-
* @param code The JavaScript code to compile.
38-
* @param options the optional options
39-
* @example vm.createScript(code[, options])
40-
*/
41-
def createScript(code: String, options: ScriptOptions = js.native): ContextifyScript = js.native
41+
def createContext(sandbox: js.Object, options: CreateContextOptions = js.native): ScriptContext = js.native
42+
def createContext(): ScriptContext = js.native
4243

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

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

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

7274
/**
@@ -76,14 +78,7 @@ trait VM extends js.Object {
7678
* @param options the optional options
7779
* @example script.runInThisContext([options])
7880
*/
79-
def runInThisContext(code: String, options: ContextOptions = js.native): ContextifyScript = js.native
80-
81-
/**
82-
* Returns true if the given sandbox object has been contextified using vm.createContext().
83-
* @param sandbox the sandbox
84-
* @return true if the given sandbox object has been contextified using vm.createContext().
85-
*/
86-
def isContext(sandbox: js.Any): Boolean = js.native
81+
def runInThisContext(code: String, options: VMRunInContextOptions = js.native): Script = js.native
8782

8883
}
8984

@@ -93,3 +88,46 @@ trait VM extends js.Object {
9388
@js.native
9489
@JSImport("vm", JSImport.Namespace)
9590
object VM extends VM
91+
92+
class CompileFunctionOptions(
93+
var filename: js.UndefOr[String] = js.undefined,
94+
var lineOffset: js.UndefOr[Int] = js.undefined,
95+
var columnOffset: js.UndefOr[Int] = js.undefined,
96+
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
97+
@deprecated("Use script.createCachedData", "Node.js v10")
98+
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
99+
var parsingContext: js.UndefOr[js.Object] = js.undefined,
100+
var contextExtensions: js.UndefOr[js.Array[js.Object]] = js.undefined
101+
) extends js.Object
102+
103+
class CreateContextOptions(
104+
var name: js.UndefOr[String] = js.undefined,
105+
var origin: js.UndefOr[String] = js.undefined,
106+
var codeGeneration: js.UndefOr[CodeGeneration] = js.undefined
107+
) extends js.Object
108+
109+
class VMRunInNewContextOptions(
110+
var filename: js.UndefOr[String] = js.undefined,
111+
var lineOffset: js.UndefOr[Int] = js.undefined,
112+
var columnOffset: js.UndefOr[Int] = js.undefined,
113+
var displayErrors: js.UndefOr[Boolean] = js.undefined,
114+
var timeout: js.UndefOr[Int] = js.undefined,
115+
var breakOnSigint: js.UndefOr[Boolean] = js.undefined,
116+
var contextName: js.UndefOr[String] = js.undefined,
117+
var contextOrigin: js.UndefOr[String] = js.undefined,
118+
var contextCodeGeneration: js.UndefOr[CodeGeneration] = js.undefined,
119+
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
120+
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
121+
var importModuleDynamically: js.UndefOr[js.Function] = js.undefined
122+
) extends js.Object
123+
124+
class VMRunInContextOptions(var filename: js.UndefOr[String] = js.undefined,
125+
var lineOffset: js.UndefOr[Int] = js.undefined,
126+
var columnOffset: js.UndefOr[Int] = js.undefined,
127+
var displayErrors: js.UndefOr[Boolean] = js.undefined,
128+
var timeout: js.UndefOr[Int] = js.undefined,
129+
var breakOnSigint: js.UndefOr[Boolean] = js.undefined,
130+
var cachedData: js.UndefOr[Uint8Array | DataView] = js.undefined,
131+
var produceCachedData: js.UndefOr[Boolean] = js.undefined,
132+
var importModuleDynamically: js.UndefOr[js.Function] = js.undefined)
133+
extends js.Object

app/nodejs-v8/src/test/scala/io/scalajs/nodejs/vm/VMTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class VMTest extends FunSpec {
2727
ExpectedData(animal = "cat", count = 12, name = "kitty12")
2828
)
2929

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

0 commit comments

Comments
 (0)