-
Notifications
You must be signed in to change notification settings - Fork 27
/
jscore_page.dart
203 lines (189 loc) · 5.39 KB
/
jscore_page.dart
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
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:flutter_jscore/flutter_jscore.dart';
class JsCorePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return JsCorePageState();
}
}
class JsCorePageState extends State<JsCorePage> {
// 输入控制器
late TextEditingController _jsInputController;
// 结果
String? _result;
// Jsc上下文
late JSContext _jsContext;
@override
void initState() {
super.initState();
// 创建js上下文
_jsContext = JSContext.createInGroup();
// 注册alert方法
_alertDartFunc = _alert;
var jsAlertFunction = JSObject.makeFunctionWithCallback(
_jsContext, 'alert', Pointer.fromFunction(alert));
_jsContext.globalObject.setProperty('alert', jsAlertFunction.toValue(),
JSPropertyAttributes.kJSPropertyAttributeNone);
// 注册flutter.print静态方法
_printDartFunc = _print;
var flutterJSClass = JSClass.create(JSClassDefinition(
version: 0,
attributes: JSClassAttributes.kJSClassAttributeNone,
className: 'flutter',
staticFunctions: [
JSStaticFunction(
name: 'print',
callAsFunction: Pointer.fromFunction(flutterPrint),
attributes: JSPropertyAttributes.kJSPropertyAttributeNone,
),
],
));
var flutterJSObject = JSObject.make(_jsContext, flutterJSClass);
_jsContext.globalObject.setProperty('flutter', flutterJSObject.toValue(),
JSPropertyAttributes.kJSPropertyAttributeDontDelete);
// 设置默认JavaScript脚本
_jsInputController = TextEditingController(text: '''
function helloJsCore()
{
var years = 2000 + 20;
alert('Hello JavaScriptCore!', years);
flutter.print('Hello JavaScriptCore!');
return 'JSCore' + (2000 + 20);
}
helloJsCore();
''');
}
@override
void dispose() {
_jsInputController.dispose();
// 释放js上下文
_jsContext.release();
super.dispose();
}
/// 绑定JavaScript alert()函数
static Pointer alert(
Pointer ctx,
Pointer function,
Pointer thisObject,
int argumentCount,
Pointer<Pointer> arguments,
Pointer<Pointer> exception) {
if (_alertDartFunc != null) {
_alertDartFunc!(
ctx, function, thisObject, argumentCount, arguments, exception);
}
return nullptr;
}
static JSObjectCallAsFunctionCallbackDart? _alertDartFunc;
Pointer _alert(
Pointer ctx,
Pointer function,
Pointer thisObject,
int argumentCount,
Pointer<Pointer> arguments,
Pointer<Pointer> exception) {
String msg = 'No Message';
if (argumentCount != 0) {
msg = '';
for (int i = 0; i < argumentCount; i++) {
if (i != 0) {
msg += '\n';
}
var jsValueRef = arguments[i];
msg += JSValue(_jsContext, jsValueRef).string!;
}
}
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Alert'),
content: Text(msg),
);
});
return nullptr;
}
/// 绑定flutter.print()函数
static Pointer flutterPrint(
Pointer ctx,
Pointer function,
Pointer thisObject,
int argumentCount,
Pointer<Pointer> arguments,
Pointer<Pointer> exception) {
if (_printDartFunc != null) {
_printDartFunc!(
ctx, function, thisObject, argumentCount, arguments, exception);
}
return nullptr;
}
static JSObjectCallAsFunctionCallbackDart? _printDartFunc;
Pointer _print(
Pointer ctx,
Pointer function,
Pointer thisObject,
int argumentCount,
Pointer<Pointer> arguments,
Pointer<Pointer> exception) {
if (argumentCount > 0) {
print(JSValue(_jsContext, arguments[0]).string);
}
return nullptr;
}
// 运行JavaScript脚本
String? _runJs(String script) {
// 运行JavaScript脚本
var jsValue = _jsContext.evaluate(script);
// 获取返回结果
return jsValue.string;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('JavaScriptCore for Flutter'),
),
body: ListView(
padding: EdgeInsets.only(top: 10.0),
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 10.0, right: 10.0),
child: Text('JavaScript:'),
),
Container(
margin: EdgeInsets.all(10.0),
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
border:
Border.all(width: 1.0, color: Theme.of(context).primaryColor),
borderRadius: BorderRadius.all(new Radius.circular(5.0)),
),
child: TextField(
controller: _jsInputController,
maxLines: 30,
style: TextStyle(
fontSize: 12.0,
),
decoration: InputDecoration(
border: InputBorder.none,
),
),
),
Container(
margin: EdgeInsets.only(left: 10.0, right: 10.0),
child: Text('Result: ${_result ?? ''}'),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_result = _runJs(_jsInputController.text);
});
},
child: Icon(Icons.autorenew),
),
);
}
}