[ARCHIVED] PluotSorbet is a J2ME-compatible virtual machine written in JavaScript.
Перейти к файлу
Michael Bebenita a02cdac210 Merge pull request #7 from syg/goddammit
Don't assign methodInfo to the function object, which has resolve hooks.
2014-12-20 02:02:21 -08:00
bench Add scimark2 benchmark. 2014-12-01 06:29:00 -08:00
certs only use test cert when running tests 2014-10-22 12:13:19 -07:00
classfile Avoid data view, cache strings. 2014-12-18 16:54:09 -08:00
java Add localmsg://nokia.product-info and MIDletRegistry.java 2014-12-12 21:01:41 +08:00
jit Merge branch 'master' of https://github.com/mbebenita/j2me.js into jit-merge-mbx 2014-12-19 10:06:47 -07:00
libs Merge master. 2014-12-17 17:25:34 -06:00
midp Merge. 2014-12-18 17:23:42 -08:00
shumway Add missing files. 2014-12-18 14:17:31 -08:00
style Add more instrumentation. 2014-12-18 00:53:16 -08:00
tests Merge branch 'master' of https://github.com/mbebenita/j2me.js into jit-merge-mbx 2014-12-19 10:06:47 -07:00
tools Move runtime classes to the root. 2014-11-24 23:53:35 -08:00
.gitignore Add files generated from Jpp to .gitignore 2014-11-19 12:37:37 +01:00
.travis.yml fix error in travis config 2014-08-04 19:59:13 -07:00
LICENSE Add licensing information for ImmutableImageFromByteArrayTest 2014-12-04 11:40:59 -06:00
Makefile Fix AOT command. 2014-12-19 15:54:14 -08:00
README.md Convert JavaExceptions into plain exceptions. 2014-12-18 18:41:07 -06:00
actors.ts Don't use forEach in class constructor. 2014-12-18 14:24:08 -08:00
bindings.ts Better hashcode fix. 2014-12-16 16:01:16 -08:00
blackBox.js Temp 2014-12-15 17:38:27 -08:00
bytecodes.ts TSify vm.js and rename to interpreter. 2014-11-27 01:15:46 -08:00
classRegistry.ts Remove allocation heavy bit tests. 2014-12-18 13:57:53 -08:00
compatibility.js Fixes chrome: 2014-11-11 16:40:32 -08:00
context.ts Merge branch 'master' of https://github.com/mbebenita/j2me.js into jit-merge-mbx 2014-12-19 10:06:47 -07:00
desktop-ui.js Show the coordinates when mousing over the canvas for debugging 2014-08-06 17:20:40 -04:00
dumplog.py Use a simple python tool to print the log file 2014-09-17 13:09:34 -07:00
frame.js Rename IP to BCI everywhere. 2014-12-01 09:47:42 -08:00
game-ui.js whitespace fixes 2014-08-05 22:11:32 -07:00
index.html use setZeroTimeout 2014-10-15 15:14:13 -07:00
index.js Merge pull request #698 from PinZhang/pzhang/no-camera 2014-12-15 10:27:17 +08:00
instrument.js Fix socket closing race. 2014-12-12 15:30:22 -06:00
interpreter.ts Merge branch 'master' of https://github.com/mbebenita/j2me.js into jit-merge-mbx 2014-12-19 10:06:47 -07:00
jsc.ts Remove references to old files. 2014-12-18 17:19:46 -08:00
jsshell.js Stub Promise in jsshell. 2014-12-19 14:36:06 -08:00
jvm.ts Refactor to avoid confusion with ctx.current(). 2014-12-17 14:04:13 -08:00
legacy.js Fix chrome again. 2014-11-20 14:52:07 -08:00
main.html Adds make aot target to compile everything. 2014-12-19 12:23:55 -08:00
main.js Add some instrumentation. 2014-12-19 00:54:42 -08:00
manifest.webapp Merge branch 'master' of https://github.com/andreasgal/j2me.js into download_jad 2014-12-11 00:57:55 +01:00
metrics.ts Merge. 2014-12-11 16:04:20 -08:00
midlet.js Allow customization of the download message. Fixes #711 2014-12-11 20:53:42 +01:00
native.js Merge. 2014-12-18 17:23:42 -08:00
opcodes.js cleanup opcode table 2014-07-12 11:48:05 -07:00
options.ts TSify more of the runtime. 2014-11-24 23:13:32 -08:00
override.js Remove createAlternateImpl. 2014-12-18 18:41:24 -06:00
references.ts Avoid data view, cache strings. 2014-12-18 16:54:09 -08:00
runtime.ts Don't assign methodInfo to the function object, which has resolve hooks. 2014-12-19 17:49:15 -08:00
signature.js Fix IN slots counting bug 2014-10-17 11:46:52 +08:00
string.js Remove createAlternateImpl. 2014-12-18 18:41:24 -06:00
timer.js add zero timer 2014-07-17 21:18:53 -07:00
types.ts Trying to get rid of interpreter's calling convention. 2014-12-01 17:01:28 -08:00
util.js Merge branch 'master' of https://github.com/mbebenita/j2me.js into jit-merge-mbx 2014-11-30 20:48:15 -08:00
utilities.ts Default to release mode. 2014-12-18 13:08:04 -08:00

README.md

j2me.js Build Status

j2me.js is a J2ME virtual machine in JavaScript.

The current goals of j2me.js are:

  1. Run MIDlets in a way that emulates the reference implementation of phone ME Feature MR4 (b01)
  2. 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 installed

Get the j2me.js repo if you don't have it already

    git clone https://github.com/andreasgal/j2me.js

Build using make:

    cd j2me.js
    make

Running apps & MIDlets, Debugging

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

See full list at libs/urlparams.js

  • 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

Example - Asteroids

    python tests/httpServer.py &
    http://localhost:8000/index.html?midletClassName=asteroids.Game&jars=tests/tests.jar&gamepad=1

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:

  • mozTCPSocket
  • mozContacts
  • mozbrowser
  • notifications

To enable this type of API for a MIDlet you're running, use Myk's API Enabler Addon

FirefoxOS device (or emulator)

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.

Once you've updated manifest.webapp, connect to the device or emulator as described in the FirefoxOS Developer Phone Guide 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, 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.

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).

Running a single test

If the test you want to run is a class with a main method, specify a main URL parameter to index.html, e.g.:

    main=gnu/testlet/vm/SystemTest&jars=tests/tests.jar

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

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.:

    args=java.lang.TestSystem,javax.crypto.TestRC4,com/nokia/mid/ui/TestVirtualKeyboard

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 &

Failures (and what to do)

Frequent causes of failure include:

  • automation.js expects a different number of tests to have passed than the number that actually passed (this is very common when adding new tests)
  • timeout: Travis machines are generally slower than dev machines and so tests that pass locally will fail in the continuous integration tests
  • 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.

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, a file called test.log gets generated. 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

  2. Execute the jsshell.js file as follows:

     js jsshell.js package.ClassName
    

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 : */

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: */

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).

When debugging using the WebIDE, enable the option "select an iframe as the currently targeted document" and select the iframe containing main.html as the debugging target. NB: you need to connect to a target device running FxOS 2.1 or up to use this feature in WebIDE.

Use these JS calls within the console to start and stop profiling (TODO: I haven't actually gotten these to work):

    Instrument.startProfile()
    Instrument.stopProfile()

It can be helpful to increase this about:config option: devtools.hud.loglimit.console

Alternatively, use the "Performance" tab of the Firefox Developer Tools.

Java profiling

j2me.js includes its own profiler that is capable of measuring the performance of Java methods running inside its JVM.

When running j2me.js in Desktop Firefox, click the "profile" button that appears below the output iframe. Press the button again to stop profiling. You should get output including the total time taken inside each method and the number of times each method was called.

Add "&profile=1" to your URL parameter list to enable profile immediately upon loading j2me.js (index.html).

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.

Uses async_storage.js (from gaia) - async API for accessing IndexedDB

Java APIs are sync but our implementations use async APIs

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.:

Native.create("java/lang/System.arraycopy.(Ljava/lang/Object;ILjava/lang/Object;II)V", function(src, srcOffset, dst, dstOffset, length) {...});

If you need to implement a method in JS but you can't declare it native in Java, use Override.

e.g.:

Override.create("com/ibm/oti/connection/file/Connection.decode.(Ljava/lang/String;)Ljava/lang/String;", function(...) {...});

If raising a Java Exception, throw new instance of Java Exception class as defined in runtime.ts, e.g.:

throw $.newNullPointerException("Cannot copy to/from a null array.");

Remember:

  • 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