pluotsorbet/README.md

213 строки
9.8 KiB
Markdown
Исходник Обычный вид История

2015-02-03 01:53:59 +03:00
# j2me.js [![Build Status](https://travis-ci.org/mozilla/j2me.js.svg)](https://travis-ci.org/mozilla/j2me.js)
2014-07-15 11:29:17 +04:00
2014-08-02 21:30:32 +04:00
j2me.js is a J2ME virtual machine in JavaScript.
2014-07-15 11:28:23 +04:00
The current goals of j2me.js are:
1. Run MIDlets in a way that emulates the reference implementation of phone ME Feature MR4 (b01)
2014-11-19 02:50:48 +03:00
1. Keep j2me.js simple and small: Leverage the phoneME JDK/infrastructure and existing Java code as much as we can, and implement as little as possible in JavaScript
## Building j2me.js
Make sure you have a [JRE](http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html) installed
2015-02-03 01:53:59 +03:00
Get the [j2me.js repo](https://github.com/mozilla/j2me.js) if you don't have it already
2015-02-03 01:53:59 +03:00
git clone https://github.com/mozilla/j2me.js
Build using make:
cd j2me.js
make
2014-11-19 02:50:48 +03:00
## Running apps & MIDlets, Debugging
2014-11-19 02:50:48 +03:00
index.html is a webapp that runs j2me.js. The URL parameters you pass to index.html control the specific behavior of j2me.js.
### URL parameters
2015-01-22 01:55:07 +03:00
You can specify URL parameters to override the configuration. See the full list of parameters at config/urlparams.js.
2014-11-19 02:50:48 +03:00
* `main` - default is `com/sun/midp/main/MIDletSuiteLoader`
* `midletClassName` - must be set to the main class to run. Only valid when default `main` parameter is used. Defaults to `RunTests`
* `autosize` - if set to `1`, j2me app will fill the page.
* `gamepad` - if set to `1`, gamepad will be visible/available.
### Desktop
To run a MIDlet on desktop, you must first start an http server that will host index.html. You can then connect to the http server, passing URL parameters to index.html
python tests/httpServer.py &
http://localhost:8000/index.html?jad=ExampleApp.jad&jars=ExampleApp.jar&midletClassName=com.example.yourClassNameHere
2014-11-19 02:50:48 +03:00
Example - Asteroids
2014-11-19 02:50:48 +03:00
python tests/httpServer.py &
http://localhost:8000/index.html?midletClassName=asteroids.Game&jars=tests/tests.jar&gamepad=1
2014-11-19 02:50:48 +03:00
Some apps require access to APIs that aren't enabled by default on Desktop Firefox and there is no UI built in to Desktop Firefox to enable them. APIs matching this description include:
2014-11-19 02:50:48 +03:00
* mozTCPSocket
* mozContacts
* mozbrowser
2014-11-20 04:11:44 +03:00
* notifications
2014-11-19 02:50:48 +03:00
To enable this type of API for a MIDlet you're running, use [Myk's API Enabler Addon](https://github.com/mykmelez/tcpsocketpup)
### FirefoxOS device (or emulator)
2014-11-20 04:11:44 +03:00
To run a MIDlet on a FirefoxOS device, update the `launch_path` property in manifest.webapp. The `midletClassName` URL parameter needs to point to an app.
2014-11-19 02:50:48 +03:00
Once you've updated manifest.webapp, connect to the device or emulator as described in the [FirefoxOS Developer Phone Guide](https://developer.mozilla.org/en-US/Firefox_OS/Developer_phone_guide/Flame) and select your j2me.js directory (the one containing manifest.webapp) when choosing the app to push to device.
Example - Asteroids
"launch_path": "/index.html?midletClassName=asteroids.Game&jars=tests/tests.jar&logConsole=web&autosize=1&gamepad=1"
## Tests
You can run the test suite with `make test`. The main driver for the test suite is automation.js which uses the Casper.js testing framework and slimer.js (a Gecko backend for casper.js). This test suite runs on every push (continuous integration) thanks to Travis.
If you want to pass additional [casperJS command line options](http://docs.slimerjs.org/current/configuration.html), look at the "test" target in Makefile and place additional command line options before the automation.js filename.
gfx tests use image comparison; a reference image is provided with the test and the output of the test must match the reference image. The output is allowed to differ from the reference image by a number of pixels specified in automation.js.
2014-11-20 04:11:44 +03:00
The main set of unit tests that automation.js runs is the set covered by the RunTests MIDlet. The full list of RunTests tests available in the tests/Testlets.java generated file. RunTests runs a number of "Testlets" (Java classes that implement the `Testlet` interface).
2014-11-19 02:50:48 +03:00
### Running a single test
2014-11-20 04:11:44 +03:00
If the test you want to run is a class with a main method, specify a `main` URL parameter to index.html, e.g.:
2014-11-20 04:11:44 +03:00
main=gnu/testlet/vm/SystemTest&jars=tests/tests.jar
2014-11-19 02:50:48 +03:00
If the test you want to run is a MIDlet, specify `midletClassName` and `jad` URL parameters to index.html (`main` will default to the MIDletSuiteLoader), e.g.:
midletClassName=tests/alarm/MIDlet1&jad=tests/midlets/alarm/alarm.jad&jars=tests/tests.jar
2014-11-20 04:11:44 +03:00
If the test you want to run is a Testlet , specify an `args` URL parameter to index.html. You can specify multiple Testlets separated by commas, and you can use either '.' or '/' in the class name, e.g.:
2014-11-19 02:50:48 +03:00
args=java.lang.TestSystem,javax.crypto.TestRC4,com/nokia/mid/ui/TestVirtualKeyboard
2014-11-20 04:11:44 +03:00
If the testlet uses sockets, you must start 4 servers (instead of just the http server):
python tests/httpServer.py &
python tests/echoServer.py &
cd tests && python httpsServer.py &
cd tests && python sslEchoServer.py &
2014-11-19 02:50:48 +03:00
### Failures (and what to do)
Frequent causes of failure include:
* timeout: Travis machines are generally slower than dev machines and so tests that pass locally will fail in the continuous integration tests
2014-11-20 04:11:44 +03:00
* Number of differing pixels in a gfx/rendering test exceeds the threshold allowed in automation.js. This will often happen because slimerJS uses a different version of Firefox than the developer. This can also happen because the test renders text, whose font rendering can vary from machine to machine, perhaps even with the same font.
2014-11-19 02:50:48 +03:00
gfx/rendering tests will print a number next to the error message. That number is the number of differing pixels. If it is close to the threshold you can probably just increase the threshold in automation.js with no ill effect.
The test output will include base64 encoded images; copy this into your browser's URL bar as a data URL to see what the actual test output looked like.
When running `make test`, verbose test output will be printed to your terminal. Check that for additional info on the failures that occurred.
## Logging
See `logConsole` and `logLevel` URL params in libs/console.js
## Running j2me.js in the SpiderMonkey shell
1. Download the [SpiderMonkey shell](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell)
1. Execute the jsshell.js file as follows:
2014-10-30 04:24:33 +03:00
js jsshell.js package.ClassName
2014-11-19 02:50:48 +03:00
## Coding Style
In general, stick with whatever style exists in the file you are modifying.
If you're creating a new file, use 4-space indents for Java and 2-space indents of JS.
Use JavaDoc to document public APIs in Java.
Modeline for Java files:
/* vim: set filetype=java shiftwidth=4 tabstop=4 autoindent cindent expandtab : */
2014-11-20 04:11:44 +03:00
Modelines for JS files:
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
2014-11-19 02:50:48 +03:00
## Profiling
### JS profiling
One way to profile j2me.js is to use the JS profiler available in Firefox Dev Tools. This will tell us how well the JVM is working and how well natives work. This type of profiling will not measure time that is taken waiting for async callbacks to be called (for example, when using the native JS filesystem API).
## Filesystem
midp/fs.js contains native implementations of various midp filesystem APIs.
Those implementations call out to lib/fs.js which is a JS implementation of a filesystem.
2014-12-23 14:30:18 +03:00
Java APIs are sync, so our implementation stores files in memory and makes them available mostly synchronously.
2014-11-19 02:50:48 +03:00
## Implementing Java functions in native code
`native` keyword tells Java that the function is implemented in native code
e.g.:
public static native long currentTimeMillis();
Java compiler will do nothing to ensure that implementation actually exists. At runtime, implementation better be available or you'll get a runtime exception.
We use `Native` object in JS to handle creation and registration of `native` functions. See native.js
Native.create("name/of/function.(parameterTypes)returnType", jsFuncToCall, isAsync)
e.g.:
2014-11-20 04:11:44 +03:00
Native.create("java/lang/System.arraycopy.(Ljava/lang/Object;ILjava/lang/Object;II)V", function(src, srcOffset, dst, dstOffset, length) {...});
2014-11-19 02:50:48 +03:00
2014-11-20 04:11:44 +03:00
If you need to implement a method in JS but you can't declare it `native` in Java, use `Override`.
2014-11-19 02:50:48 +03:00
e.g.:
2015-02-06 23:55:04 +03:00
Override["java/lang/Math.min.(II)I"] = function(a, b) {
return Math.min(a, b);
};
2014-11-19 02:50:48 +03:00
If raising a Java `Exception`, throw new instance of Java `Exception` class as defined in runtime.ts, e.g.:
2014-11-19 02:50:48 +03:00
throw $.newNullPointerException("Cannot copy to/from a null array.");
2014-11-19 02:50:48 +03:00
2014-11-20 04:11:44 +03:00
Remember:
2014-11-19 02:50:48 +03:00
2014-11-20 04:11:44 +03:00
* Return types are automatically converted to Java types, but parameters are not automatically converted from Java types to JS types
* Pass `true` as last param if JS will make async calls and return a `Promise`
* `this` will be available in any context that `this` would be available to the Java method. i.e. `this` will be `null` for `static` methods.
* Context is last param to every function registered using `Native.create` or `Override.create`
* Parameter types are specified in [JNI](http://www.iastate.edu/~java/docs/guide/nativemethod/types.doc.html)
## Packaging
The repository includes tools for packaging j2me.js into an Open Web App.
It's possible to simply package the entire contents of your working directory,
but these tools will produce a better app.
### Compiling With AOT Compiler
`make aot` compiles some Java code into JavaScript with an ahead-of-time (AOT) compiler.
To use it, first install a recent version of the
[JavaScript shell](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell).
### Compiling With Closure
`make closure` compiles some JavaScript code with the Closure compiler.
2015-02-04 02:15:53 +03:00
To use it, first download this custom version of the compiler to tools/closure.jar:
```
2015-02-04 02:15:53 +03:00
wget https://github.com/mykmelez/closure-compiler/releases/download/v0.1/closure.jar -P tools/
```