forked from mozilla/pluotsorbet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
native.js
1087 lines (942 loc) · 33.3 KB
/
native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
'use strict';
var asyncImplStringAsync = "Async";
function asyncImpl(returnKind, promise) {
var ctx = $.ctx;
promise.then(function(res) {
if (returnKind === "J" || returnKind === "D") {
ctx.current().stack.push2(res);
} else if (returnKind !== "V") {
ctx.current().stack.push(res);
} else {
// void, do nothing
}
J2ME.Scheduler.enqueue(ctx);
}, function(exception) {
var classInfo = CLASSES.getClass("org/mozilla/internal/Sys");
var methodInfo = classInfo.getMethodByNameString("throwException", "(Ljava/lang/Exception;)V", true);
ctx.pushFrame(Frame.create(methodInfo, [exception]));
J2ME.Scheduler.enqueue(ctx);
});
$.pause(asyncImplStringAsync);
}
function preemptingImpl(returnKind, returnValue) {
if (J2ME.Scheduler.shouldPreempt()) {
asyncImpl(returnKind, Promise.resolve(returnValue));
return;
}
return returnValue;
}
var Native = {};
Native["java/lang/System.arraycopy.(Ljava/lang/Object;ILjava/lang/Object;II)V"] = function(src, srcOffset, dst, dstOffset, length) {
if (!src || !dst)
throw $.newNullPointerException("Cannot copy to/from a null array.");
var srcKlass = src.klass;
var dstKlass = dst.klass;
if (!srcKlass.isArrayKlass || !dstKlass.isArrayKlass)
throw $.newArrayStoreException("Can only copy to/from array types.");
if (srcOffset < 0 || (srcOffset+length) > src.length || dstOffset < 0 || (dstOffset+length) > dst.length || length < 0)
throw $.newArrayIndexOutOfBoundsException("Invalid index.");
var srcIsPrimitive = !(src instanceof Array);
var dstIsPrimitive = !(dst instanceof Array);
if ((srcIsPrimitive && dstIsPrimitive && srcKlass !== dstKlass) ||
(srcIsPrimitive && !dstIsPrimitive) ||
(!srcIsPrimitive && dstIsPrimitive)) {
throw $.newArrayStoreException("Incompatible component types: " + srcKlass + " -> " + dstKlass);
}
if (!dstIsPrimitive) {
if (srcKlass != dstKlass && !J2ME.isAssignableTo(srcKlass.elementKlass, dstKlass.elementKlass)) {
var copy = function(to, from) {
var obj = src[from];
if (obj && !J2ME.isAssignableTo(obj.klass, dstKlass.elementKlass)) {
throw $.newArrayStoreException("Incompatible component types.");
}
dst[to] = obj;
};
if (dst !== src || dstOffset < srcOffset) {
for (var n = 0; n < length; ++n)
copy(dstOffset++, srcOffset++);
} else {
dstOffset += length;
srcOffset += length;
for (var n = 0; n < length; ++n)
copy(--dstOffset, --srcOffset);
}
return;
}
}
if (dst !== src || dstOffset < srcOffset) {
for (var n = 0; n < length; ++n)
dst[dstOffset++] = src[srcOffset++];
} else {
dstOffset += length;
srcOffset += length;
for (var n = 0; n < length; ++n)
dst[--dstOffset] = src[--srcOffset];
}
};
var stubProperties = {
"com.nokia.multisim.slots": "1",
"com.nokia.mid.imsi": "000000000000000",
"com.nokia.mid.imei": "",
};
Native["java/lang/System.getProperty0.(Ljava/lang/String;)Ljava/lang/String;"] = function(key) {
key = J2ME.fromJavaString(key);
var value;
switch (key) {
case "microedition.encoding":
// The value of this property is different than the value on a real Nokia Asha 503 phone.
// On the phone, it is: ISO8859_1.
// If we changed this, we would need to remove the optimizations for UTF_8_Reader and
// UTF_8_Writer and optimize the ISO8859_1 alternatives.
value = "UTF-8";
break;
case "microedition.io.file.FileConnection.version":
value = "1.0";
break;
case "microedition.locale":
value = navigator.language;
break;
case "microedition.platform":
value = config.platform ? config.platform : "Nokia503/14.0.4/java_runtime_version=Nokia_Asha_1_2";
break;
case "microedition.platformimpl":
value = null;
break;
case "microedition.profiles":
value = "MIDP-2.1"
break;
case "microedition.pim.version":
value = "1.0";
break;
case "microedition.amms.version":
value = "1.1";
break;
case "microedition.media.version":
value = '1.2';
break;
case "mmapi-configuration":
value = null;
break;
case "fileconn.dir.memorycard":
value = "file:///MemoryCard/";
break;
// The names here should be localized.
case "fileconn.dir.memorycard.name":
value = "Memory card";
break;
case "fileconn.dir.private":
value = "file:///Private/";
break;
case "fileconn.dir.private.name":
value = "Private";
break;
case "fileconn.dir.applications.bookmarks":
value = null;
break;
case "fileconn.dir.received":
value = "file:///Phone/_my_downloads/";
break;
case "fileconn.dir.received.name":
value = "Downloads";
break;
case "fileconn.dir.photos":
value = "file:///Phone/_my_pictures/";
break;
case "fileconn.dir.photos.name":
value = "Photos";
break;
case "fileconn.dir.videos":
value = "file:///Phone/_my_videos/";
break;
case "fileconn.dir.videos.name":
value = "Videos";
break;
case "fileconn.dir.recordings":
value = "file:///Phone/_my_recordings/";
break;
case "fileconn.dir.recordings.name":
value = "Recordings";
break;
case "fileconn.dir.roots.names":
value = MIDP.fsRootNames.join(";");
break;
case "fileconn.dir.roots.external":
value = MIDP.fsRoots.map(function(v) { return "file:///" + v }).join("\n");
break;
case "file.separator":
value = "/";
break;
case "com.sun.cldc.util.j2me.TimeZoneImpl.timezone":
// Date.toString() returns something like the following:
// "Wed Sep 17 2014 12:11:23 GMT-0700 (PDT)"
//
// Per http://www.spectrum3847.org/frc2013api/com/sun/cldc/util/j2me/TimeZoneImpl.html,
// timezones can be of the format GMT+0600, which is what this
// regex currently matches. (Those actually in GMT would not
// match the regex, causing the default "GMT" to be returned.)
// If we find this to be a problem, we could alternately return the
// zone name as provided in parenthesis, but that seems locale-specific.
var match = /GMT[+-]\d+/.exec(new Date().toString());
value = (match && match[0]) || "GMT";
break;
case "javax.microedition.io.Connector.protocolpath":
value = "com.sun.midp.io";
break;
case "javax.microedition.io.Connector.protocolpath.fallback":
value = "com.sun.cldc.io";
break;
case "com.nokia.keyboard.type":
value = "None";
break;
case "com.nokia.mid.batterylevel":
// http://developer.nokia.com/community/wiki/Checking_battery_level_in_Java_ME
value = Math.floor(navigator.battery.level * 100).toString();
break;
case "com.nokia.mid.ui.version":
value = "1.7";
break;
case "com.nokia.mid.mnc":
if (mobileInfo.icc.mcc && mobileInfo.icc.mnc) {
// The concatenation of the MCC and MNC for the ICC (i.e. SIM card).
value = util.pad(mobileInfo.icc.mcc, 3) + util.pad(mobileInfo.icc.mnc, 3);
} else {
value = null;
}
break;
case "com.nokia.mid.networkID":
if (mobileInfo.network.mcc && mobileInfo.network.mnc) {
// The concatenation of MCC and MNC for the network.
value = util.pad(mobileInfo.network.mcc, 3) + util.pad(mobileInfo.network.mnc, 3);
} else {
value = null;
}
break;
case "com.nokia.mid.ui.customfontsize":
value = "true";
break;
case "classpathext":
value = null;
break;
case "supports.audio.capture":
value = "true";
break;
case "supports.video.capture":
value = "true";
break;
case "supports.recording":
value = "true";
break;
case "audio.encodings":
value = "encoding=audio/amr";
break;
case "video.snapshot.encodings":
// FIXME Some MIDlets pass a string that contains lots of constraints
// as the `imageType` which is not yet handled in DirectVideo.jpp, let's
// just put the whole string here as a workaround and fix this in issue #688.
value = "encoding=jpeg&quality=80&progressive=true&type=jfif&width=400&height=400";
break;
default:
if (MIDP.additionalProperties[key]) {
value = MIDP.additionalProperties[key];
} else if (typeof stubProperties[key] !== "undefined") {
value = stubProperties[key];
} else {
console.warn("UNKNOWN PROPERTY (java/lang/System): " + key);
stubProperties[key] = value = null;
}
break;
}
return J2ME.newString(value);
};
Native["java/lang/System.currentTimeMillis.()J"] = function() {
return Long.fromNumber(Date.now());
};
Native["com/sun/cldchi/jvm/JVM.unchecked_char_arraycopy.([CI[CII)V"] = function(src, srcOffset, dst, dstOffset, length) {
dst.set(src.subarray(srcOffset, srcOffset + length), dstOffset);
};
Native["com/sun/cldchi/jvm/JVM.unchecked_int_arraycopy.([II[III)V"] = function(src, srcOffset, dst, dstOffset, length) {
dst.set(src.subarray(srcOffset, srcOffset + length), dstOffset);
};
Native["com/sun/cldchi/jvm/JVM.unchecked_obj_arraycopy.([Ljava/lang/Object;I[Ljava/lang/Object;II)V"] = function(src, srcOffset, dst, dstOffset, length) {
if (dst !== src || dstOffset < srcOffset) {
for (var n = 0; n < length; ++n)
dst[dstOffset++] = src[srcOffset++];
} else {
dstOffset += length;
srcOffset += length;
for (var n = 0; n < length; ++n)
dst[--dstOffset] = src[--srcOffset];
}
};
Native["com/sun/cldchi/jvm/JVM.monotonicTimeMillis.()J"] = function() {
return Long.fromNumber(performance.now());
};
Native["java/lang/Object.getClass.()Ljava/lang/Class;"] = function() {
return $.getRuntimeKlass(this.klass).classObject;
};
Native["java/lang/Object.wait.(J)V"] = function(timeout) {
$.ctx.wait(this, timeout.toNumber());
};
Native["java/lang/Object.notify.()V"] = function() {
$.ctx.notify(this);
};
Native["java/lang/Object.notifyAll.()V"] = function() {
$.ctx.notify(this, true);
};
Native["java/lang/Class.getSuperclass.()Ljava/lang/Class;"] = function() {
var superKlass = this.runtimeKlass.templateKlass.superKlass;
if (!superKlass) {
return null;
}
return superKlass.classInfo.getClassObject();
};
Native["java/lang/Class.invoke_clinit.()V"] = function() {
var classInfo = this.runtimeKlass.templateKlass.classInfo;
var className = classInfo.getClassNameSlow();
var clinit = classInfo.staticInitializer;
J2ME.preemptionLockLevel++;
if (clinit && clinit.classInfo.getClassNameSlow() === className) {
$.ctx.executeFrame(Frame.create(clinit, []));
}
};
Native["java/lang/Class.invoke_verify.()V"] = function() {
// There is currently no verification.
};
Native["java/lang/Class.init9.()V"] = function() {
$.setClassInitialized(this.runtimeKlass);
J2ME.preemptionLockLevel--;
};
Native["java/lang/Class.getName.()Ljava/lang/String;"] = function() {
return J2ME.newString(this.runtimeKlass.templateKlass.classInfo.getClassNameSlow().replace(/\//g, "."));
};
Native["java/lang/Class.forName0.(Ljava/lang/String;)V"] = function(name) {
var classInfo = null;
try {
if (!name)
throw new J2ME.ClassNotFoundException();
var className = J2ME.fromJavaString(name).replace(/\./g, "/");
classInfo = CLASSES.getClass(className);
} catch (e) {
if (e instanceof (J2ME.ClassNotFoundException))
throw $.newClassNotFoundException("'" + e.message + "' not found.");
throw e;
}
// The following can trigger an unwind.
J2ME.classInitCheck(classInfo);
};
Native["java/lang/Class.forName1.(Ljava/lang/String;)Ljava/lang/Class;"] = function(name) {
var className = J2ME.fromJavaString(name).replace(/\./g, "/");
var classInfo = CLASSES.getClass(className);
var classObject = classInfo.getClassObject();
return classObject;
};
Native["java/lang/Class.newInstance0.()Ljava/lang/Object;"] = function() {
if (this.runtimeKlass.templateKlass.classInfo.isInterface ||
this.runtimeKlass.templateKlass.classInfo.isAbstract) {
throw $.newInstantiationException("Can't instantiate interfaces or abstract classes");
}
if (this.runtimeKlass.templateKlass.classInfo instanceof J2ME.ArrayClassInfo) {
throw $.newInstantiationException("Can't instantiate array classes");
}
return new this.runtimeKlass.templateKlass;
};
Native["java/lang/Class.newInstance1.(Ljava/lang/Object;)V"] = function(o) {
// The following can trigger an unwind.
var methodInfo = o.klass.classInfo.getLocalMethodByNameString("<init>", "()V", false);
if (!methodInfo) {
throw $.newInstantiationException("Can't instantiate classes without a nullary constructor");
}
J2ME.getLinkedMethod(methodInfo).call(o);
};
Native["java/lang/Class.isInterface.()Z"] = function() {
return this.runtimeKlass.templateKlass.classInfo.isInterface ? 1 : 0;
};
Native["java/lang/Class.isArray.()Z"] = function() {
return this.runtimeKlass.templateKlass.classInfo instanceof J2ME.ArrayClassInfo ? 1 : 0;
};
Native["java/lang/Class.isAssignableFrom.(Ljava/lang/Class;)Z"] = function(fromClass) {
if (!fromClass)
throw $.newNullPointerException();
return J2ME.isAssignableTo(fromClass.runtimeKlass.templateKlass, this.runtimeKlass.templateKlass) ? 1 : 0;
};
Native["java/lang/Class.isInstance.(Ljava/lang/Object;)Z"] = function(obj) {
return obj && J2ME.isAssignableTo(obj.klass, this.runtimeKlass.templateKlass) ? 1 : 0;
};
Native["java/lang/Float.floatToIntBits.(F)I"] = (function() {
var fa = new Float32Array(1);
var ia = new Int32Array(fa.buffer);
return function(val) {
fa[0] = val;
return ia[0];
}
})();
Native["java/lang/Double.doubleToLongBits.(D)J"] = (function() {
var da = new Float64Array(1);
var ia = new Int32Array(da.buffer);
return function(val) {
da[0] = val;
return Long.fromBits(ia[0], ia[1]);
}
})();
Native["java/lang/Float.intBitsToFloat.(I)F"] = (function() {
var fa = new Float32Array(1);
var ia = new Int32Array(fa.buffer);
return function(val) {
ia[0] = val;
return fa[0];
}
})();
Native["java/lang/Double.longBitsToDouble.(J)D"] = (function() {
var da = new Float64Array(1);
var ia = new Int32Array(da.buffer);
return function(l) {
ia[0] = l.low_;
ia[1] = l.high_;
return da[0];
}
})();
Native["java/lang/Throwable.fillInStackTrace.()V"] = function() {
this.stackTrace = [];
$.ctx.frames.forEach(function(frame) {
if (!frame.methodInfo)
return;
var methodInfo = frame.methodInfo;
var methodName = methodInfo.name;
if (!methodName)
return;
var classInfo = methodInfo.classInfo;
var className = classInfo.getClassNameSlow();
this.stackTrace.unshift({ className: className, methodName: methodName, methodSignature: methodInfo.signature, offset: frame.bci });
}.bind(this));
};
Native["java/lang/Throwable.obtainBackTrace.()Ljava/lang/Object;"] = function() {
var result = null;
if (this.stackTrace) {
var depth = this.stackTrace.length;
var classNames = J2ME.newObjectArray(depth);
var methodNames = J2ME.newObjectArray(depth);
var methodSignatures = J2ME.newObjectArray(depth);
var offsets = J2ME.newIntArray(depth);
this.stackTrace.forEach(function(e, n) {
classNames[n] = J2ME.newString(e.className);
methodNames[n] = J2ME.newString(e.methodName);
methodSignatures[n] = J2ME.newString(e.methodSignature);
offsets[n] = e.offset;
});
result = J2ME.newObjectArray(3);
result[0] = classNames;
result[1] = methodNames;
result[2] = methodSignatures;
result[3] = offsets;
}
return result;
};
Native["java/lang/Runtime.freeMemory.()J"] = function() {
return Long.fromInt(0x800000);
};
Native["java/lang/Runtime.totalMemory.()J"] = function() {
return Long.fromInt(0x1000000);
};
Native["java/lang/Runtime.gc.()V"] = function() {
};
Native["java/lang/Math.floor.(D)D"] = function(val) {
return Math.floor(val);
};
Native["java/lang/Math.asin.(D)D"] = function(val) {
return Math.asin(val);
};
Native["java/lang/Math.acos.(D)D"] = function(val) {
return Math.acos(val);
};
Native["java/lang/Math.atan.(D)D"] = function(val) {
return Math.atan(val);
};
Native["java/lang/Math.atan2.(DD)D"] = function(x, y) {
return Math.atan2(x, y);
};
Native["java/lang/Math.sin.(D)D"] = function(val) {
return Math.sin(val);
};
Native["java/lang/Math.cos.(D)D"] = function(val) {
return Math.cos(val);
};
Native["java/lang/Math.tan.(D)D"] = function(val) {
return Math.tan(val);
};
Native["java/lang/Math.sqrt.(D)D"] = function(val) {
return Math.sqrt(val);
};
Native["java/lang/Math.ceil.(D)D"] = function(val) {
return Math.ceil(val);
};
Native["java/lang/Math.floor.(D)D"] = function(val) {
return Math.floor(val);
};
Native["java/lang/Thread.currentThread.()Ljava/lang/Thread;"] = function() {
return $.ctx.thread;
};
Native["java/lang/Thread.setPriority0.(II)V"] = function(oldPriority, newPriority) {
if (this.ctx) {
this.ctx.priority = newPriority;
}
};
Native["java/lang/Thread.start0.()V"] = function() {
// The main thread starts during bootstrap and don't allow calling start()
// on already running threads.
if (this === $.ctx.runtime.mainThread || this.alive)
throw $.newIllegalThreadStateException();
this.alive = true;
this.pid = util.id();
// Create a context for the thread and start it.
var newCtx = new Context($.ctx.runtime);
newCtx.thread = this;
this.ctx = newCtx;
newCtx.priority = this.priority;
var classInfo = CLASSES.getClass("org/mozilla/internal/Sys");
var run = classInfo.getMethodByNameString("runThread", "(Ljava/lang/Thread;)V", true);
newCtx.start([Frame.create(run, [ this ])]);
}
Native["java/lang/Thread.isAlive.()Z"] = function() {
return this.alive ? 1 : 0;
};
Native["java/lang/Thread.sleep.(J)V"] = function(delay) {
asyncImpl("V", new Promise(function(resolve, reject) {
window.setTimeout(resolve, delay.toNumber());
}));
};
Native["java/lang/Thread.yield.()V"] = function() {
$.yield("Thread.yield");
};
Native["java/lang/Thread.activeCount.()I"] = function() {
return $.ctx.runtime.threadCount;
};
var consoleBuffer = "";
function flushConsoleBuffer() {
if (consoleBuffer.length) {
var temp = consoleBuffer;
consoleBuffer = "";
console.info(temp);
}
}
console.print = function(ch) {
if (ch === 10) {
flushConsoleBuffer();
} else {
consoleBuffer += String.fromCharCode(ch);
}
};
Native["com/sun/cldchi/io/ConsoleOutputStream.write.(I)V"] = function(ch) {
console.print(ch);
};
Native["com/sun/cldc/io/ResourceInputStream.open.(Ljava/lang/String;)Ljava/lang/Object;"] = function(name) {
var fileName = J2ME.fromJavaString(name);
var data = JARStore.loadFile(fileName);
var obj = null;
if (data) {
obj = J2ME.newObject(CLASSES.java_lang_Object.klass);
obj.data = data;
obj.pos = 0;
}
return obj;
};
Native["com/sun/cldc/io/ResourceInputStream.clone.(Ljava/lang/Object;)Ljava/lang/Object;"] = function(source) {
var obj = J2ME.newObject(CLASSES.java_lang_Object.klass);
obj.data = new Uint8Array(source.data);
obj.pos = source.pos;
return obj;
};
Native["com/sun/cldc/io/ResourceInputStream.bytesRemain.(Ljava/lang/Object;)I"] = function(handle) {
return handle.data.length - handle.pos;
};
Native["com/sun/cldc/io/ResourceInputStream.readByte.(Ljava/lang/Object;)I"] = function(handle) {
return (handle.data.length - handle.pos > 0) ? handle.data[handle.pos++] : -1;
};
Native["com/sun/cldc/io/ResourceInputStream.readBytes.(Ljava/lang/Object;[BII)I"] = function(handle, b, off, len) {
var data = handle.data;
var remaining = data.length - handle.pos;
if (len > remaining)
len = remaining;
for (var n = 0; n < len; ++n)
b[off+n] = data[handle.pos+n];
handle.pos += len;
return (len > 0) ? len : -1;
};
Native["java/lang/ref/WeakReference.initializeWeakReference.(Ljava/lang/Object;)V"] = function(target) {
this.target = target;
};
Native["java/lang/ref/WeakReference.get.()Ljava/lang/Object;"] = function() {
return this.target ? this.target : null;
};
Native["java/lang/ref/WeakReference.clear.()V"] = function() {
this.target = null;
};
Native["com/sun/cldc/isolate/Isolate.registerNewIsolate.()V"] = function() {
this.id = util.id();
};
Native["com/sun/cldc/isolate/Isolate.getStatus.()I"] = function() {
return this.runtime ? this.runtime.status : J2ME.RuntimeStatus.New;
};
Native["com/sun/cldc/isolate/Isolate.nativeStart.()V"] = function() {
$.ctx.runtime.jvm.startIsolate(this);
};
Native["com/sun/cldc/isolate/Isolate.waitStatus.(I)V"] = function(status) {
asyncImpl("V", new Promise((function(resolve, reject) {
var runtime = this.runtime;
if (runtime.status >= status) {
resolve();
return;
}
function waitForStatus() {
if (runtime.status >= status) {
resolve();
return;
}
runtime.waitStatus(waitForStatus);
}
waitForStatus();
}).bind(this)));
};
Native["com/sun/cldc/isolate/Isolate.currentIsolate0.()Lcom/sun/cldc/isolate/Isolate;"] = function() {
return $.ctx.runtime.isolate;
};
Native["com/sun/cldc/isolate/Isolate.getIsolates0.()[Lcom/sun/cldc/isolate/Isolate;"] = function() {
var isolates = J2ME.newObjectArray(Runtime.all.keys().length);
var n = 0;
Runtime.all.forEach(function (runtime) {
isolates[n++] = runtime.isolate;
});
return isolates;
};
Native["com/sun/cldc/isolate/Isolate.id0.()I"] = function() {
return this.id;
};
Native["com/sun/cldc/isolate/Isolate.setPriority0.(I)V"] = function(newPriority) {
};
Native["com/sun/cldc/i18n/j2me/UTF_8_Reader.init.([B)V"] = function(data) {
this.decoded = new TextDecoder("UTF-8").decode(data);
};
Native["com/sun/cldc/i18n/j2me/UTF_8_Reader.readNative.([CII)I"] = function(cbuf, off, len) {
if (this.decoded.length === 0) {
return -1;
}
for (var i = 0; i < len; i++) {
cbuf[i + off] = this.decoded.charCodeAt(i);
}
this.decoded = this.decoded.substring(len);
return len;
};
Native["java/io/DataOutputStream.UTFToBytes.(Ljava/lang/String;)[B"] = function(jStr) {
var str = J2ME.fromJavaString(jStr);
var utflen = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
utflen++;
} else if (c > 0x07FF) {
utflen += 3;
} else {
utflen += 2;
}
}
if (utflen > 65535) {
throw $.newUTFDataFormatException();
}
var count = 0;
var bytearr = J2ME.newByteArray(utflen + 2);
bytearr[count++] = (utflen >>> 8) & 0xFF;
bytearr[count++] = (utflen >>> 0) & 0xFF;
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
bytearr[count++] = c;
} else if (c > 0x07FF) {
bytearr[count++] = 0xE0 | ((c >> 12) & 0x0F);
bytearr[count++] = 0x80 | ((c >> 6) & 0x3F);
bytearr[count++] = 0x80 | ((c >> 0) & 0x3F);
} else {
bytearr[count++] = 0xC0 | ((c >> 6) & 0x1F);
bytearr[count++] = 0x80 | ((c >> 0) & 0x3F);
}
}
return bytearr;
};
Native["com/sun/cldc/i18n/j2me/UTF_8_Writer.encodeUTF8.([CII)[B"] = function(cbuf, off, len) {
var outputArray = [];
var pendingSurrogate = this.pendingSurrogate;
var inputChar = 0;
var outputSize = 0;
var count = 0;
while (count < len) {
var outputByte = new Int8Array(4); // Never more than 4 encoded bytes
inputChar = 0xffff & cbuf[off + count];
if (0 != pendingSurrogate) {
if (0xdc00 <= inputChar && inputChar <= 0xdfff) {
//000u uuuu xxxx xxxx xxxx xxxx
//1101 10ww wwxx xxxx 1101 11xx xxxx xxxx
var highHalf = (pendingSurrogate & 0x03ff) + 0x0040;
var lowHalf = inputChar & 0x03ff;
inputChar = (highHalf << 10) | lowHalf;
} else {
// write replacement value instead of unpaired surrogate
outputByte[0] = replacementValue;
outputSize = 1;
outputArray.push(outputByte.subarray(0, outputSize));
}
pendingSurrogate = 0;
}
if (inputChar < 0x80) {
outputByte[0] = inputChar;
outputSize = 1;
} else if (inputChar < 0x800) {
outputByte[0] = 0xc0 | ((inputChar >> 6) & 0x1f);
outputByte[1] = 0x80 | (inputChar & 0x3f);
outputSize = 2;
} else if (0xd800 <= inputChar && inputChar <= 0xdbff) {
pendingSurrogate = inputChar;
outputSize = 0;
} else if (0xdc00 <= inputChar && inputChar <= 0xdfff) {
// unpaired surrogate
outputByte[0] = replacementValue;
outputSize = 1;
} else if (inputChar < 0x10000) {
outputByte[0] = 0xe0 | ((inputChar >> 12) & 0x0f);
outputByte[1] = 0x80 | ((inputChar >> 6) & 0x3f);
outputByte[2] = 0x80 | (inputChar & 0x3f);
outputSize = 3;
} else {
/* 21 bits: 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
* a aabb bbbb cccc ccdd dddd
*/
outputByte[0] = 0xf0 | ((inputChar >> 18) & 0x07);
outputByte[1] = 0x80 | ((inputChar >> 12) & 0x3f);
outputByte[2] = 0x80 | ((inputChar >> 6) & 0x3f);
outputByte[3] = 0x80 | (inputChar & 0x3f);
outputSize = 4;
}
outputArray.push(outputByte.subarray(0, outputSize));
count++;
}
this.pendingSurrogate = pendingSurrogate;
var totalSize = outputArray.reduce(function(total, cur) {
return total + cur.length;
}, 0);
var res = J2ME.newByteArray(totalSize);
outputArray.reduce(function(total, cur) {
res.set(cur, total);
return total + cur.length;
}, 0);
return res;
};
Native["com/sun/cldc/i18n/j2me/UTF_8_Writer.sizeOf.([CII)I"] = function(cbuf, off, len) {
var inputChar = 0;
var outputSize = 0;
var outputCount = 0;
var count = 0;
var localPendingSurrogate = this.pendingSurrogate;
while (count < len) {
inputChar = 0xffff & cbuf[off + count];
if (0 != localPendingSurrogate) {
if (0xdc00 <= inputChar && inputChar <= 0xdfff) {
//000u uuuu xxxx xxxx xxxx xxxx
//1101 10ww wwxx xxxx 1101 11xx xxxx xxxx
var highHalf = (localPendingSurrogate & 0x03ff) + 0x0040;
var lowHalf = inputChar & 0x03ff;
inputChar = (highHalf << 10) | lowHalf;
} else {
// going to write replacement value instead of unpaired surrogate
outputSize = 1;
outputCount += outputSize;
}
localPendingSurrogate = 0;
}
if (inputChar < 0x80) {
outputSize = 1;
} else if (inputChar < 0x800) {
outputSize = 2;
} else if (0xd800 <= inputChar && inputChar <= 0xdbff) {
localPendingSurrogate = inputChar;
outputSize = 0;
} else if (0xdc00 <= inputChar && inputChar <= 0xdfff) {
// unpaired surrogate
// going to output replacementValue;
outputSize = 1;
} else if (inputChar < 0x10000) {
outputSize = 3;
} else {
/* 21 bits: 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
* a aabb bbbb cccc ccdd dddd
*/
outputSize = 4;
}
outputCount += outputSize;
count++;
}
return outputCount;
};
Native["com/sun/j2me/content/AppProxy.midletIsAdded.(ILjava/lang/String;)V"] = function(suiteId, className) {
console.warn("com/sun/j2me/content/AppProxy.midletIsAdded.(ILjava/lang/String;)V not implemented");
};
Native["com/nokia/mid/impl/jms/core/Launcher.handleContent.(Ljava/lang/String;)V"] = function(content) {
var fileName = J2ME.fromJavaString(content);
var ext = fileName.split('.').pop().toLowerCase();
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#Supported_image_formats
if (["jpg", "jpeg", "gif", "apng", "png", "bmp", "ico"].indexOf(ext) == -1) {
console.error("File not supported: " + fileName);
throw $.newException("File not supported: " + fileName);
}
// `fileName` is supposed to be a full path, but we don't support
// partition, e.g. `C:` or `E:` etc, so the `fileName` we got here
// is something like: `Photos/sampleImage.jpg`, we need to prepend
// the root dir to make sure it's valid.
var imgData = fs.getBlob("/" + fileName);
if (!imgData) {
console.error("File not found: " + fileName);
throw $.newException("File not found: " + fileName);
}
var maskId = "image-launcher";
var mask = document.getElementById(maskId);
function _revokeImageURL() {
URL.revokeObjectURL(/url\((.+)\)/ig.exec(mask.style.backgroundImage)[1]);
}
if (mask) {
_revokeImageURL();
} else {
mask = document.createElement("div");
mask.id = maskId;
mask.onclick = mask.ontouchstart = function() {
_revokeImageURL();
mask.parentNode.removeChild(mask);
};
document.getElementById("main").appendChild(mask);
}
mask.style.backgroundImage = "url(" +
URL.createObjectURL(imgData) + ")";
};
function addUnimplementedNative(signature, returnValue) {
var doNotWarn;
if (typeof returnValue === "function") {
doNotWarn = returnValue;
} else {
doNotWarn = function() { return returnValue };
}
var warnOnce = function() {
console.warn(signature + " not implemented");
warnOnce = doNotWarn;
return doNotWarn();
};
Native[signature] = function() { return warnOnce() };
}
Native["org/mozilla/internal/Sys.eval.(Ljava/lang/String;)V"] = function(src) {
if (!release) {
eval(J2ME.fromJavaString(src));
}
};
var profileStarted = false;
Native["org/mozilla/internal/Sys.startProfile.()V"] = function() {
if (profile === 4) {
if (!profileStarted) {
profileStarted = true;
console.log("Start profile at: " + performance.now());
startTimeline();
}
}
};
var profileSaved = false;
Native["org/mozilla/internal/Sys.stopProfile.()V"] = function() {
if (profile === 4) {
if (!profileSaved) {
profileSaved = true;
console.log("Stop profile at: " + performance.now());
setZeroTimeout(function() {
stopAndSaveTimeline();
});
}
}
};
Native["java/io/ByteArrayOutputStream.write.([BII)V"] = function(b, off, len) {
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length)) {
throw $.newIndexOutOfBoundsException();
}
if (len == 0) {
return;
}
var count = this.count;
var buf = this.buf;
var newcount = count + len;
if (newcount > buf.length) {
var newbuf = J2ME.newByteArray(Math.max(buf.length << 1, newcount));
newbuf.set(buf);
buf = newbuf;
this.buf = buf;
}