Coder Social home page Coder Social logo

html-bridge's Introduction

Strongly-Typed Javascript (STJS)

Build Status

STJS is an open source (Apache 2.0 licensed) Javascript code generator from a Java source. It is built as a Maven plugin that can be executed after the compilation of your Java code.

Our full website can be found at http://st-js.org

Compiling the Project

ST-JS compiles with the traditional mvn install command, but it currently needs both Java 6 - that is the default JDK on the command line when you call the Maven command, but also Java 8 to compile the generator-plugin-java8 artifact. To achieve this, you need to to configure the environment variable JAVA8_HOME that points to the home of your JDK 8 home folder.

Notes on Primitives+Arrays Support

Arrays

AnyNonPrimitiveType[] is Array

String[] a = {"a", "b"};
var a = ["a", "b"];

Multidimensional arrays

float[][][] arr = new float[1][2][3];
Object[][][] objarr = new Object[][][]{ new String[][]{ { "hello" }, { "world" }, new String[]{} }, new Integer[][]{ { 1, 2 }, { 3, 4 } }, new Double[4][5] };
var arr = Array.apply(null, Array(1)).map(function(){return Array.apply(null, Array(2)).map(function(){return new Float32Array(3);});});"
var objarr = [[["hello"], ["world"], []], [[1, 2], [3, 4]], Array.apply(null, Array(4)).map(function() { return Array(5);})];

Primitive Arrays

Java JavaScript
boolean[] Int8Array
byte[] Int8Array
short[] Int16Array
char[] Uint16Array
int[] Int32Array
float[] Float32Array
double[] Float64Array
long[] Array

Foreach

Enhanced for loop over Java arrays will iterate over values instead of keys

String[] msg = {"hello", "world"};
for(String s : msg) {
	console.log(s);
}
var msg = ["hello", "world"];
for(var index$s = 0, arr$s = msg; index$s < arr$s.length; index$s++) {
	var s = arr$s[index$s];
	console.log(s);
}

long

There is no support for 64bit integer type in JavaScript. Max value for an integer is 2^53-1

> Number.MAX_SAFE_INTEGER
9007199254740991

char

char is stored in Java as unsigned short so it is in st-js primitive support.

char[] in st-js is Uint16Array.

The following side effects appear:

String a = "hello worl";
char c = 'd';
String result = a + c;
var a = "hello worl";
var c = 'd'.charCodeAt(0); //looks ugly but compatible
var result = a + String.fromCharCode(c);
char c = 'a';
byte b = (byte) c;
var c = 'a'.charCodeAt(0);
var b = c << 24 >> 24;

float and double

Are considered equivalent no attempt for Java compatibility is made.

primitive type casting

Java JavaScript
int to short ((i)<<16>>16)
int to byte ((i)<<24>>24)
int to char ((i)&0xfff)
int to long var l = i
int to float var f = i
int to double var d = i
short to byte ((s)<<24>>24)
short to char ((s)&0xfff)
short to int var i = s
short to long var l = s
short to float var f = s
short to double var d = s
byte to short var s = b
byte to char var c = b
byte to int var i = b
byte to long var l = b
byte to float var f = b
byte to double var d = b
char to byte ((c)<<24>>24)
char to short var s = c
char to int var i = c
char to long var l = c
char to float var f = c
char to double var d = c
long to byte ((l)<<24>>24)
long to short ((l)<<16>>16)
long to char ((l)&0xfff)
long to int var i = l|0
long to float var f = l
long to double var d = l
float to byte ((f)<<24>>24)
float to short ((f)<<16>>16)
float to char ((f)&0xfff)
float to int var i = f|0
float to long var l = stjs.trunc(f)
float to double var d = f
double to byte ((d)<<24>>24)
double to short ((d)<<16>>16)
double to char ((d)&0xfff)
double to int var i = d|0
double to long var l = stjs.trunc(d)
double to float var f = d

html-bridge's People

Contributors

acraciun avatar asvk avatar chexov avatar chugai avatar npiguet avatar sonthanh avatar twitwi avatar zhuker avatar zhukovka avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

html-bridge's Issues

Error compiling from git

Hi folks,

I got an error when executing mvn install for the html-bridge module:

[INFO] Compilation failure

/home/florian/projects/html-bridge/src/main/java/org/stjs/javascript/dom/canvas/CanvasPixelArray.java:[5,47] error: interface expected here

/home/florian/projects/html-bridge/src/main/java/org/stjs/javascript/Global.java:[18,1] error: cannot find symbol

could not parse error message:   symbol: class GlobalScope
/home/florian/projects/html-bridge/src/main/java/org/stjs/javascript/Storage.java:3: error: interface expected here
abstract public class Storage implements Map<String, Object> {
                                            ^

/home/florian/projects/html-bridge/src/main/java/org/stjs/javascript/Global.java:[40,48] error: cannot find symbol

could not parse error message:   symbol:   class Callback0
  location: class Global
/home/florian/projects/html-bridge/src/main/java/org/stjs/javascript/Global.java:44: error: cannot find symbol
    public native static TimeoutHandler setInterval(Callback0 expr, int timeoutMillis);

It seems that some symbols have moved, or am I missing something?

Generic version of DOMEvent

This is not an issue but rather a feature discussion.

Very often, listener receive a DOMEvent and in a typical javascript application, we would use e.target to get the target of the event and do things with it. I was thinking that DOMEvent could be made generic, as in DOMEvent<TargetType> to avoid needing casts when using e.target.
This, however, has impact on a big part of the API so I don't know if it is a good solution.

Some fields and method in org.stjs.javascript.Global don't belong there

The org.stjs.javascript.Global object is currently defined in the HTML bridge. This is fine for people who use ST-JS for web project, but will cause problems for people who attempt to use ST-JS for something else (with NodeJs for example).

In particular, the Javascript specification mentions that the following fields and methods are part of the "Global Object" and defined in the language spec and should therefore be available in any JavaScript environment, not only in an HTML environment.

NaN
Infinity
undefined
eval (x)
parseInt (string , radix)
parseFloat (string)
isNaN (number)
isFinite (number)
decodeURI (encodedURI)
decodeURIComponent (encodedURIComponent)
encodeURI (uri)
encodeURIComponent (uriComponent)

There are also a bunch of constructor properties that should be defined, but maybe these should be defined somewhere else with ST-JS.

I think a Global object should be created in the "shared" module to contain those fields and functions, and they should be removed from the Global object in the HTML Bridge. Maybe the HTML's Global could then extend the shared Global.

References: Sections 15.1.2 and 15.1.3 of the ECMA-262 Specification.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.