Difference between revisions of "JavaScript"

From PowerUI
Jump to: navigation, search
(Created page with "PowerUI currently has two engines for runtime code - Nitro (script type='text/nitro') and Nitrassic (script type='text/javascript-x'). Nitro is a custom language which more re...")
 
(Can I run x - How compliant is the JavaScript in PowerUI?)
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
PowerUI currently has two engines for runtime code - Nitro (script type='text/nitro') and Nitrassic (script type='text/javascript-x'). Nitro is a custom language which more resembles UnityScript and the current version of Nitrassic is mostly compliant with ECMAScript 5. Nitro, the older engine, is now defunct and will be removed in the near future so you should either code using C# or use type='text/javascript-x' in your script tags.
+
PowerUI presently runs Javascript via Jint and an implementation of WebAssembly. It previously included a series of custom Javascript engines which were fast but ultimately fell short when it came to standards compliance.
  
 
== The JavaScript challenge - A short history ==
 
== The JavaScript challenge - A short history ==
  
'''Most gaming platforms, including iOS, explicitly don't allow runtime compliation.''' JavaScript was designed to be runtime compiled so these restrictions cause an instant major problem. It's not like it's a particularly small issue either - all but two of the Unity platforms (Standalone and Android) don't allow runtime compilation. The only route to get JavaScript working on those platforms is to compile the JavaScript 'ahead of time' in the editor. However, that is ''extremely'' difficult for a language as dynamic as JavaScript is. We initially went the same route as Unity - create a JavaScript-like language which can be easily compiled in the editor. That's Nitro. If you've ever used UnityScript (or Nitro itself) you will notice very quickly that you can't simply run a library like jQuery, or indeed virtually any actual JavaScript from the web ''(it's still very unfortunate that Unity still calls UnityScript 'JavaScript' and uses the .js extension)''. So over the years as PowerUI's support for the web in general has increased, we naturally needed to make a new JavaScript engine - one which supported ahead of time compilation and as much of the JavaScript specification as possible. That's Nitrassic.
+
'''Most gaming platforms, including iOS, explicitly don't allow runtime compliation.''' JavaScript was designed to be runtime compiled so these restrictions cause an instant major problem. It's not like it's a particularly small issue either - all but two of the Unity platforms (Standalone and Android) don't allow runtime compilation. The only route to get JavaScript working on those platforms is to compile the JavaScript 'ahead of time' in the editor. However, that is ''extremely'' difficult for a language as dynamic as JavaScript is. We initially went the same route as Unity - create a JavaScript-like language which can be easily compiled in the editor. That was Nitro.
 +
 
 +
If you've ever used UnityScript (or Nitro itself) you will notice very quickly that you can't simply run a library like jQuery, or indeed virtually any actual JavaScript from the web ''(it's still very unfortunate that Unity still calls UnityScript 'JavaScript' and uses the .js extension)''. So over the years as PowerUI's support for the web in general has increased, we naturally needed to make a new JavaScript engine - one which supported ahead of time compilation and as much of the JavaScript specification as possible. That's Nitrassic.
  
 
Nitrassic was built by taking the [https://github.com/paulbartrum/jurassic Jurassic engine] and merging it with Nitro (thus the name) - adding a type tracking system to make it suitable for editor compilation. This ultimately had the effect of making Jurassic massively faster, but at the expense of months of high complexity compiler work. We're in new territory here - Nitrassic is the first ahead-of-time JavaScript compiler which could have big implications for things like Node.js in the wider web world; particularly as, when combined with IL2CPP, the result is JavaScript running at near native speeds.
 
Nitrassic was built by taking the [https://github.com/paulbartrum/jurassic Jurassic engine] and merging it with Nitro (thus the name) - adding a type tracking system to make it suitable for editor compilation. This ultimately had the effect of making Jurassic massively faster, but at the expense of months of high complexity compiler work. We're in new territory here - Nitrassic is the first ahead-of-time JavaScript compiler which could have big implications for things like Node.js in the wider web world; particularly as, when combined with IL2CPP, the result is JavaScript running at near native speeds.
 +
 +
Since then however, running Javascript has become a little easier so presently PowerUI runs Jint for full standards compliance.
  
 
== Can I run x - How compliant is the JavaScript in PowerUI? ==
 
== Can I run x - How compliant is the JavaScript in PowerUI? ==
  
The best option? Just try it and see what happens! At the moment, this depends on:
+
The best option? Just try it and see what happens! At the moment, this depends on the Web API's that you intend to use. PowerUI currently has broad support for a variety of web APIs. The API coverage is enough such that jQuery and basic React works for example.
* Further testing is required.
+
 
* If your JavaScript uses the 'delete' keyword (jQuery does) then tracking the types becomes undefined, but it will still try anyway.
+
== Interaction with the rest of your code ==
* How much of the core Object interface 'x' uses. Primarily that's prototype. Whilst prototypes are supported, anObject.prototype (which is part of that interface) currently isn't. You can access these instance prototypes via a constructor function instead:
+
 
 +
Running Javascript is a little useless if you can't make it interact with your game, and vice versa. Here's how to get that going.
 +
 
 +
=== Calling C#/Unity methods from JS ===
 +
 
 +
You'll need the classes you want to use to be inside a namespace. From there, you can then import the namespace like this:
  
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
  
function Point(x,y){
+
// You can use the Unity API directly too:
    // 'this' is equivalent of Point.prototype
+
var UnityEngine = importNamespace("UnityEngine");
    this.x=x;
 
    this.y=y;
 
}
 
  
var p = new Point(114,20);
+
var go = new UnityEngine.GameObject();
 +
 
 +
go.name = "MadeWithLoveByRealJavaScript";
  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
We're working on the above points and any help with that would be greatly appreciated!
+
=== Calling JS from C# ===
 +
 
 +
To call JS functions from C#, use document.Run:
 +
 
 +
<syntaxhighlight lang="javascript">
 +
var document = UI.document;
 +
document.Run("FunctionName", Arguments);
 +
</syntaxhighlight>

Latest revision as of 00:18, 6 September 2018

PowerUI presently runs Javascript via Jint and an implementation of WebAssembly. It previously included a series of custom Javascript engines which were fast but ultimately fell short when it came to standards compliance.

The JavaScript challenge - A short history

Most gaming platforms, including iOS, explicitly don't allow runtime compliation. JavaScript was designed to be runtime compiled so these restrictions cause an instant major problem. It's not like it's a particularly small issue either - all but two of the Unity platforms (Standalone and Android) don't allow runtime compilation. The only route to get JavaScript working on those platforms is to compile the JavaScript 'ahead of time' in the editor. However, that is extremely difficult for a language as dynamic as JavaScript is. We initially went the same route as Unity - create a JavaScript-like language which can be easily compiled in the editor. That was Nitro.

If you've ever used UnityScript (or Nitro itself) you will notice very quickly that you can't simply run a library like jQuery, or indeed virtually any actual JavaScript from the web (it's still very unfortunate that Unity still calls UnityScript 'JavaScript' and uses the .js extension). So over the years as PowerUI's support for the web in general has increased, we naturally needed to make a new JavaScript engine - one which supported ahead of time compilation and as much of the JavaScript specification as possible. That's Nitrassic.

Nitrassic was built by taking the Jurassic engine and merging it with Nitro (thus the name) - adding a type tracking system to make it suitable for editor compilation. This ultimately had the effect of making Jurassic massively faster, but at the expense of months of high complexity compiler work. We're in new territory here - Nitrassic is the first ahead-of-time JavaScript compiler which could have big implications for things like Node.js in the wider web world; particularly as, when combined with IL2CPP, the result is JavaScript running at near native speeds.

Since then however, running Javascript has become a little easier so presently PowerUI runs Jint for full standards compliance.

Can I run x - How compliant is the JavaScript in PowerUI?

The best option? Just try it and see what happens! At the moment, this depends on the Web API's that you intend to use. PowerUI currently has broad support for a variety of web APIs. The API coverage is enough such that jQuery and basic React works for example.

Interaction with the rest of your code

Running Javascript is a little useless if you can't make it interact with your game, and vice versa. Here's how to get that going.

Calling C#/Unity methods from JS

You'll need the classes you want to use to be inside a namespace. From there, you can then import the namespace like this:

// You can use the Unity API directly too:
var UnityEngine = importNamespace("UnityEngine");

var go = new UnityEngine.GameObject();

go.name = "MadeWithLoveByRealJavaScript";

Calling JS from C#

To call JS functions from C#, use document.Run:

var document = UI.document;
document.Run("FunctionName", Arguments);