Difference between revisions of "JavaScript"

From PowerUI
Jump to: navigation, search
(Can I run x - How compliant is the JavaScript in PowerUI?)
Line 14: Line 14:
 
* Further testing is required.
 
* Further testing is required.
 
* If you 'collapse' a variable - this happens when you set it to things of more than one type and the type tracker is forced to stop tracking it. Not all dynamic resolve paths are complete so your success here will vary. In general though collapsed variables are an indication of incorrect code so the engine will at least tell you about them.
 
* If you 'collapse' a variable - this happens when you set it to things of more than one type and the type tracker is forced to stop tracking it. Not all dynamic resolve paths are complete so your success here will vary. In general though collapsed variables are an indication of incorrect code so the engine will at least tell you about them.
* If your JavaScript uses the 'delete' keyword (jQuery does) then tracking the types becomes undefined, but it will still try anyway (jQuery fails).
+
 
 +
<syntaxhighlight lang="javascript">
 +
 
 +
var a=14;
 +
 
 +
function Hello(){
 +
    a="My String";
 +
}
 +
 
 +
// A is both a string and a number. It 'collapses' to Object.
 +
 
 +
</syntaxhighlight>
 +
 
 +
* If your JavaScript uses the 'delete' keyword (jQuery does) then tracking the types becomes undefined, but it will still try anyway (jQuery fails because of the below point).
 
* 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:
 
* 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:
  

Revision as of 02:46, 21 April 2017

PowerUI currently has three engines for runtime code - Nitro (script type='text/nitro'), Nitrassic (script type='text/javascript-x') and WebAssembly. 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.

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.

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.

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:

  • Further testing is required.
  • If you 'collapse' a variable - this happens when you set it to things of more than one type and the type tracker is forced to stop tracking it. Not all dynamic resolve paths are complete so your success here will vary. In general though collapsed variables are an indication of incorrect code so the engine will at least tell you about them.
var a=14;

function Hello(){
    a="My String";
}

// A is both a string and a number. It 'collapses' to Object.
  • If your JavaScript uses the 'delete' keyword (jQuery does) then tracking the types becomes undefined, but it will still try anyway (jQuery fails because of the below point).
  • 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:
function Point(x,y){
    // 'this' is equivalent of Point.prototype
    this.x=x;
    this.y=y;
}

var p = new Point(114,20);

We're working on the above points and any help with that would be greatly appreciated!