-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.js
108 lines (86 loc) · 2.88 KB
/
interface.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
/**
* This file shows how to implement a interface with javascript function
*/
var assert = require('assert');
var java = require('../');
var vm = java.createVm();
var runCalled = false;
var RunnableImpl = vm.implement(['java/lang/Runnable'], {
'run()V': function () {
console.log('run()V called');
assert(arguments.length === 0);
runCalled = true
}
});
var runnable = RunnableImpl.newInstance();
runnable.invoke('run()V');
assert(runCalled);
var TestImpl = vm.implement({
'voidMethod(ZBCI)V': function (z, b, c, i) {
console.log('voidMethod', z, b, c, i);
assert(z);
assert.strictEqual(b, 12);
assert.strictEqual(c, 97);
assert.strictEqual(i, 2015);
}, 'stringMethod(ILjava/lang/String;D)Ljava/lang/String;': function (i, s, d) {
assert.strictEqual(i, 34);
assert.strictEqual(s, "Hello world");
assert.strictEqual(d, Math.PI);
return i + s + d;
},
'longMethod(J)J': function (i) {
assert.strictEqual(i, 1e16);
return Date.now();
},
'throws()V': function () {
throw new Error("foo bar");
},
'test()V': function () {
},
'stringMethod(Ljava/lang/String;)Ljava/lang/String;': function (s) {
return s + ':' + typeof s;
}
});
var test = TestImpl.newInstance();
test.invoke('voidMethod(ZBCI)V', true, 12, 'a', 2015);
var _e = false;
try {
test.invoke('throws()V');
} catch (e) {
assert.strictEqual(e.message, 'java.lang.RuntimeException: Uncaught Error: foo bar');
_e = true;
}
assert(_e);
assert.strictEqual(
test.invoke('stringMethod(ILjava/lang/String;D)Ljava/lang/String;', 34, 'Hello world', Math.PI),
'34Hello world' + Math.PI
);
assert.strictEqual(
test.invoke('stringMethod(Ljava/lang/String;)Ljava/lang/String;', null),
'null:object'
);
var diff = Date.now() - test.invoke('longMethod(J)J', 1e16);
assert(diff >= -1 && diff <= 0);
// extends super class
var written = [];
var myWriter = vm.findClass('java/io/PrintStream').newInstance('Ljava/io/OutputStream;', vm.implement('java/io/OutputStream', {
'write(I)V': function (ch) {
written.push(ch);
process.stdout.write(new Buffer([ch]));
}
}).newInstance());
myWriter.invoke('print(Ljava/lang/String;)V', 'Hello world!');
assert.strictEqual(new Buffer(written).toString(), 'Hello world!');
var threadStarted = false;
var timer = setTimeout(function () {
assert(threadStarted);
}, 1000);
var Thread = vm.findClass('java/lang/Thread').newInstance('Ljava/lang/Runnable;', vm.implement(['java/lang/Runnable'], {
'run()V': function () {
assert(arguments.length === 0);
threadStarted = true;
clearTimeout(timer);
console.log('thread started');
}
}).newInstance());
Thread.invoke('start()V');