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
//! A library for interacting with the contract phat-quickjs
//!
//! The available JS APIs can be found [here](https://github.com/Phala-Network/phat-quickjs/blob/master/npm_package/pink-env/src/index.ts).
//!
//! # Script args and return value
//!
//! The `eval_*` functions take a script as source code and args as input. It eval the source by delegate call to the code pointed to by
//! driver JsDelegate2 and return the value of the js expresson. The return value only lost-less supports string or Uint8Array. Ojbects
//! of other types will be casted to string.
//!
//! Example:
//! ```no_run
//! let src = r#"
//! (function () {
//! return scriptArgs[0] + scriptArgs[1];
//! })()
//! "#;
//! let output = phat_js::eval(src, &["foo".into(), "bar".into()]);
//! assert_eq!(output, Ok(phat_js::Output::String("foobar".into())));
//! ```
//!
//! # JS API examples
//!
//! ## Cross-contract call
//!
//! ```js
//! // Delegate calling
//! const delegateOutput = pink.invokeContractDelegate({
//! codeHash:
//! "0x0000000000000000000000000000000000000000000000000000000000000000",
//! selector: 0xdeadbeef,
//! input: "0x00",
//! });
//!
//! // Instance calling
//! const contractOutput = pink.invokeContract({
//! callee: "0x0000000000000000000000000000000000000000000000000000000000000000",
//! input: "0x00",
//! selector: 0xdeadbeef,
//! gasLimit: 0n,
//! value: 0n,
//! });
//! ```
//!
//! ## HTTP request
//!
//! HTTP request is supported in the JS environment. However, the API is sync rather than async.
//! This is different from other JavaScript engines. For example:
//! ```js
//! const response = pink.httpReqeust({
//! url: "https://httpbin.org/ip",
//! method: "GET",
//! returnTextBody: true,
//! });
//! console.log(response.body);
//! ```
//!
//! ## SCALE codec
//!
//! Let's introduce the details of the SCALE codec API which is not documented in the above link.
//!
//! The SCALE codec API is mounted on the global object `pink.SCALE` which contains the following functions:
//!
//! - `pink.SCALE.parseTypes(types: string): TypeRegistry`
//! - `pink.SCALE.codec(type: string | number | number[], typeRegistry?: TypeRegistry): Codec`
//!
//! Let's make a basice example to show how to use the SCALE codec API:
//!
//! ```js
//! const types = `
//! Hash=[u8;32]
//! Info={hash:Hash,size:u32}
//! `;
//! const typeRegistry = pink.SCALE.parseTypes(types);
//! const infoCodec = pink.SCALE.codec(`Info`, typeRegistry);
//! const encoded = infoCodec.encode({
//! hash: "0x1234567890123456789012345678901234567890123456789012345678901234",
//! size: 1234,
//! });
//! console.log("encoded:", encoded);
//! const decoded = infoCodec.decode(encoded);
//! pink.inspect("decoded:", decoded);
//! ```
//!
//! The above code will output:
//! ```text
//! JS: encoded: 18,52,86,120,144,18,52,86,120,144,18,52,86,120,144,18,52,86,120,144,18,52,86,120,144,18,52,86,120,144,18,52,210,4,0,0
//! JS: decoded: {
//! JS: hash: 0x1234567890123456789012345678901234567890123456789012345678901234,
//! JS: size: 1234
//! JS: }
//! ```
//!
//! ## Grammar of the type definition
//! ### Basic grammar
//! In the above example, we use the following type definition:
//! ```text
//! Hash=[u8;32]
//! Info={hash:Hash,size:u32}
//! ```
//! where we define a type `Hash` which is an array of 32 bytes, and a type `Info` which is a struct containing a `Hash` and a `u32`.
//!
//! The grammar is defined as follows:
//!
//! Each entry is type definition, which is of the form `name=type`. Then name must be a valid identifier,
//! and type is a valid type expression described below.
//!
//! Type expression can be one of the following:
//!
//! | Type Expression | Description | Example |
//! |-------------------------|-------------------------------------------|------------------|
//! | `#`-prefixed | Primitive types. | `#bool`, `#u8`, `#u16`, `#u32`, `#u64`, `#u128`, `#i8`, `#i16`, `#i32`, `#i64`, `#i128`, `#str` |
//! | `[type;size]` | Array type with element type `type` and size `size`. | `[#u8; 32]` |
//! | `(type1, type2, ...)` | Tuple type with elements of type `type1`, `type2`, ... | `(#u8, #str)` |
//! | `{field1:type1, field2:type2, ...}` | Struct type with fields and types. | `{age:#u32, name:#str}` |
//! | `<variant1:type1, variant2:type2, ...>` | Enum type with variants and types. if the variant is a unit variant, then the type expression can be omitted.| `<Success:#i32, Error:#str>`, `<Some:#u32,None>` |
//!
//! ### Nested type definition
//!
//! Type definition can be nested, for example:
//!
//! ```text
//! Block={header:{hash:[u8;32],size:u32}}
//! ```
//!
//! ### Generic type support
//!
//! Generic parameters can be added to the type definition, for example:
//!
//! ```text
//! Result<T,E>=<Ok:T,Err:E>
//! ```
//!
//! ### Direct encode/decode API
//!
//! The encode/decode api also support literal type definition as well as a typename or id, for example:
//!
//! ```js
//! const data = { name: "Alice", age: 18 };
//! const encoded = pink.SCALE.encode(data, "{ name: #str, age: u8 }");
//! const decoded = pink.SCALE.decode(encoded, "{ name: #str, age: u8 }");
//! ```
//!
//! ## Error handling
//! Host calls would throw an exception if any error is encountered. For example, if we pass an invalid method to the API:
//! ```js
//! try {
//! const response = pink.httpReqeust({
//! url: "https://httpbin.org/ip",
//! method: 42,
//! returnTextBody: true,
//! });
//! console.log(response.body);
//! } catch (err) {
//! console.log("Some error ocurred:", err);
//! }
//! ```
//!
extern crate alloc;
use String;
use Vec;
use call;
use Hash;
use ;
pub type RefValue<'a> = ;
pub type Value = ;
pub type Output = Value;
/// Evaluate a script with the default delegate contract code
/// Evaluate a compiled bytecode with the default delegate contract code
/// Evaluate multiple scripts with the default delegate contract code
/// Evaluate a script with given delegate contract code
/// Evaluate a compiled script with given delegate contract code
/// Evaluate multiple scripts with given delegate
/// Compile a script with the default delegate contract
/// Compile a script with given delegate contract