Bck2BrwsrMangling
From APIDesign
(Difference between revisions)
(→Like JNI) |
(→Like JNI) |
||
Line 3: | Line 3: | ||
== Like [[JNI]] == | == Like [[JNI]] == | ||
- | There is a common [[wikipedia:Name_mangling|mangling scheme]] specified by [[JNI]] for [[C]] and [[Bck2Brwsr]] mimics the [http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/design.html specification] as closely as possible. | + | There is a common [[wikipedia:Name_mangling|mangling scheme]] specified by [[JNI]] for [[C]] and [[Bck2Brwsr]] mimics the [http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/design.html specification] as closely as possible. The mangling is based on ''underscore encoding'' substitution: |
+ | |||
+ | * Fully qualified name uses '_' to separate package names and class name | ||
+ | * There is "__" after name of a method and before its arguments | ||
+ | * return type is encoded first, parameters follow | ||
+ | * If there is an '_' in the name segment, it gets replaced by "_1" | ||
+ | * array signatures start with '[' - such character is replaced by "_3" | ||
+ | * object signatures end with ';' - that character is replaced by "_2" | ||
+ | |||
+ | As a result to call method String.substring(int, int) returning String would be written as: | ||
+ | |||
+ | <source lang="javascript"> | ||
+ | var s = "..."; | ||
+ | var r = s.substring__Ljava_lang_String_2II(0, 5); | ||
+ | </source> | ||
+ | |||
+ | When calling a static method, one first needs to obtain the name of a class. The class is made available in a global object called "vm". As such calling ''String.valueOf(10)'' is translated to: | ||
+ | |||
+ | <source lang="javascript"> | ||
+ | var clazz = vm.java_lang_String(false); | ||
+ | var r = clazz.valueOf__Ljava_lang_String_2I(10); | ||
+ | </source> |
Revision as of 13:22, 26 January 2013
When translating the ByteCode to JavaScript the Bck2Brwsr project needs to face a common problem -- when translating a typed language (like Java) to untyped (like JavaScript) one needs to mange the names, so they continue to support method and field overloading.
Like JNI
There is a common mangling scheme specified by JNI for C and Bck2Brwsr mimics the specification as closely as possible. The mangling is based on underscore encoding substitution:
- Fully qualified name uses '_' to separate package names and class name
- There is "__" after name of a method and before its arguments
- return type is encoded first, parameters follow
- If there is an '_' in the name segment, it gets replaced by "_1"
- array signatures start with '[' - such character is replaced by "_3"
- object signatures end with ';' - that character is replaced by "_2"
As a result to call method String.substring(int, int) returning String would be written as:
var s = "..."; var r = s.substring__Ljava_lang_String_2II(0, 5);
When calling a static method, one first needs to obtain the name of a class. The class is made available in a global object called "vm". As such calling String.valueOf(10) is translated to:
var clazz = vm.java_lang_String(false); var r = clazz.valueOf__Ljava_lang_String_2I(10);