j2s/README.md

30 lines
1.6 KiB
Markdown
Raw Permalink Normal View History

2023-08-05 15:37:18 +00:00
# JScript
**NOTE: This had nothing to do with Microsoft's dialect of EcmaScript**
2023-12-24 12:31:50 +00:00
**WARNING: Currently, this code is undocumented. Proceed with caution and a psychiatrist.**
2023-08-05 15:37:18 +00:00
JScript is an engine, capable of running EcmaScript 5, written entirely in Java. This engine has been developed with the goal of being easy to integrate with your preexisting codebase, **THE GOAL OF THIS ENGINE IS NOT PERFORMANCE**. My crude experiments show that this engine is 50x-100x slower than V8, which, although bad, is acceptable for most simple scripting purposes. Note that although the codebase has a Main class, this isn't meant to be a standalone program, but instead a library for running JavaScript code.
2023-08-05 15:37:18 +00:00
## Example
2023-12-24 12:31:50 +00:00
The following is going to execute a simple javascript statement:
2023-08-05 15:37:18 +00:00
```java
2023-12-24 12:31:50 +00:00
var engine = new Engine(false);
// Initialize a standard environment, with implementations of most basic standard libraries (Object, Array, Symbol, etc.)
var env = Internals.apply(new Environment());
// Queue code to load internal libraries and start engine
2023-12-24 12:31:50 +00:00
var awaitable = engine.pushMsg(false, env, new Filename("tmp", "eval"), "10 + Math.sqrt(5 / 3)", null);
// Run the engine on the same thread, until the event loop runs empty
engine.run(true);
// Get our result
System.out.println(awaitable.await());
2023-08-05 15:37:18 +00:00
```
2023-12-24 12:31:50 +00:00
## NOTE:
To setup the typescript bundle in your sources, run `node build.js init-ts`. This will download the latest version of typescript, minify it, and add it to your src/assets folder. If you are going to work with the `node build.js debug|release` command, this is not a necessary step.