FiendHunter commited on
Commit
f437d77
Β·
verified Β·
1 Parent(s): 9dcae8c

Delete output

Browse files
output/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- .env
2
- upload.py
3
-
 
 
 
 
output/local_repo/docs/llamaedge_docs.txt DELETED
The diff for this file is too large to render. See raw diff
 
output/local_repo/docs/quick_js.txt DELETED
@@ -1,900 +0,0 @@
1
- Directory Structure:
2
-
3
- └── ./
4
- β”œβ”€β”€ embed_js
5
- β”‚ β”œβ”€β”€ src
6
- β”‚ β”‚ └── main.rs
7
- β”‚ β”œβ”€β”€ Cargo.toml
8
- β”‚ └── README.md
9
- β”œβ”€β”€ embed_js_module
10
- β”‚ β”œβ”€β”€ src
11
- β”‚ β”‚ └── main.rs
12
- β”‚ β”œβ”€β”€ async_demo.js
13
- β”‚ β”œβ”€β”€ Cargo.toml
14
- β”‚ └── README.md
15
- β”œβ”€β”€ embed_rust_module
16
- β”‚ β”œβ”€β”€ src
17
- β”‚ β”‚ └── main.rs
18
- β”‚ β”œβ”€β”€ Cargo.toml
19
- β”‚ └── README.md
20
- β”œβ”€β”€ embed_use_es6_module
21
- β”‚ β”œβ”€β”€ src
22
- β”‚ β”‚ └── main.rs
23
- β”‚ β”œβ”€β”€ Cargo.toml
24
- β”‚ └── es6_module_demo.js
25
- β”œβ”€β”€ host_function
26
- β”‚ β”œβ”€β”€ src
27
- β”‚ β”‚ └── main.rs
28
- β”‚ β”œβ”€β”€ wasmedge_c
29
- β”‚ β”‚ └── demo_wasmedge.c
30
- β”‚ β”œβ”€β”€ Cargo.toml
31
- β”‚ └── README.md
32
- └── js_extend.rs
33
-
34
-
35
-
36
- ---
37
- File: /embed_js/src/main.rs
38
- ---
39
-
40
- use wasmedge_quickjs::*;
41
-
42
- fn main() {
43
- let mut ctx = Context::new();
44
- js_hello(&mut ctx);
45
- run_js_code(&mut ctx);
46
- run_js_function(&mut ctx);
47
- run_rust_function(&mut ctx);
48
- rust_new_object_and_js_call(&mut ctx);
49
- js_new_object_and_rust_call(&mut ctx);
50
- js_promise(&mut ctx);
51
- }
52
-
53
- fn js_hello(ctx: &mut Context) {
54
- println!("\n<----run_simple_js---->");
55
- let code = r#"print('hello quickjs')"#;
56
- let r = ctx.eval_global_str(code);
57
- println!("return value:{:?}", r);
58
- }
59
-
60
- fn run_js_code(ctx: &mut Context) {
61
- println!("\n<----run_js_code---->");
62
- let code = r#"
63
- let a = 1+1;
64
- print('js print: 1+1=',a);
65
- 'hello'; // eval_return
66
- "#;
67
- let r = ctx.eval_global_str(code);
68
- println!("return value:{:?}", r);
69
- }
70
-
71
- fn run_js_function(ctx: &mut Context) {
72
- println!("\n<----run_js_function---->");
73
- let code = r#"
74
- (x)=>{
75
- print("js print: x=",x)
76
- }
77
- "#;
78
- let r = ctx.eval_global_str(code);
79
- println!("return value:{:?}", r);
80
- if let JsValue::Function(f) = r {
81
- let hello_str = ctx.new_string("hello");
82
- let mut argv = vec![hello_str.into()];
83
- let r = f.call(&mut argv);
84
- println!("return value:{:?}", r);
85
- }
86
-
87
- let code = r#"
88
- (x)=>{
89
- print("\nx=",x)
90
- let old_value = x[0]
91
- x[0] = 1
92
- return old_value
93
- }
94
- "#;
95
- let r = ctx.eval_global_str(code);
96
- if let JsValue::Function(f) = r {
97
- let mut x = ctx.new_array();
98
- x.set(0, 0.into());
99
- x.set(1, 1.into());
100
- x.set(2, 2.into());
101
-
102
- let mut argv = vec![x.into()];
103
- println!("argv = {:?}", argv);
104
- let r = f.call(&mut argv);
105
- println!("return value:{:?}", r);
106
- }
107
- }
108
-
109
- fn run_rust_function(ctx: &mut Context) {
110
- println!("\n<----run_rust_function---->");
111
-
112
- struct HelloFn;
113
- impl JsFn for HelloFn {
114
- fn call(_ctx: &mut Context, _this_val: JsValue, argv: &[JsValue]) -> JsValue {
115
- println!("hello from rust");
116
- println!("argv={:?}", argv);
117
- JsValue::UnDefined
118
- }
119
- }
120
- let f = ctx.new_function::<HelloFn>("hello");
121
- ctx.get_global().set("hi", f.into());
122
- let code = r#"hi(1,2,3)"#;
123
- let r = ctx.eval_global_str(code);
124
- println!("return value:{:?}", r);
125
- }
126
-
127
- fn rust_new_object_and_js_call(ctx: &mut Context) {
128
- println!("\n<----rust_new_object_and_js_call---->");
129
- let mut obj = ctx.new_object();
130
- obj.set("a", 1.into());
131
- obj.set("b", ctx.new_string("abc").into());
132
-
133
- struct ObjectFn;
134
- impl JsFn for ObjectFn {
135
- fn call(_ctx: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
136
- println!("hello from rust");
137
- println!("argv={:?}", argv);
138
- if let JsValue::Object(obj) = this_val {
139
- let obj_map = obj.to_map();
140
- println!("this={:#?}", obj_map);
141
- }
142
- JsValue::UnDefined
143
- }
144
- }
145
-
146
- let f = ctx.new_function::<ObjectFn>("anything");
147
- obj.set("f", f.into());
148
-
149
- ctx.get_global().set("test_obj", obj.into());
150
-
151
- let code = r#"
152
- print('test_obj keys=',Object.keys(test_obj))
153
- print('test_obj.a=',test_obj.a)
154
- print('test_obj.b=',test_obj.b)
155
- test_obj.f(1,2,3,"hi")
156
- "#;
157
-
158
- ctx.eval_global_str(code);
159
- }
160
-
161
- fn js_new_object_and_rust_call(ctx: &mut Context) {
162
- println!("\n<----js_new_object_and_rust_call---->");
163
- let code = r#"
164
- let obj = {
165
- a:1,
166
- b:"abc",
167
- f(x){
168
- print('this=',Object.keys(this))
169
- print('x=',x)
170
- print('something_from_rust=',this.something_from_rust)
171
- }
172
- }
173
- obj
174
- "#;
175
- if let JsValue::Object(mut obj) = ctx.eval_global_str(code) {
176
- let mut args = vec![ctx.new_string("rust_args_string").into()];
177
-
178
- let obj_map = obj.to_map();
179
- println!("{:#?}", obj_map);
180
-
181
- if let Ok(o) = obj_map {
182
- println!("---call function---");
183
- if let Some(JsValue::Function(f)) = o.get("f") {
184
- f.call(&mut args);
185
- }
186
- }
187
- obj.set("something_from_rust", 255.into());
188
- println!("---call function from obj---");
189
- obj.invoke("f", &mut args);
190
- }
191
- }
192
-
193
- fn js_promise(ctx: &mut Context) {
194
- println!("\n<----promise---->");
195
- let code = r#"
196
- async function f1(){
197
- print("f1 running")
198
- return 1
199
- }
200
- async function f(){
201
- print("f running")
202
- let f1_result = await f1();
203
- print("await f1")
204
- return f1_result
205
- };
206
- f
207
- "#;
208
-
209
- let r = ctx.eval_global_str(code);
210
- println!("{:?}", r);
211
- if let JsValue::Function(f) = r {
212
- let mut args = vec![];
213
- let r = f.call(&mut args);
214
- println!("{:?}", r);
215
- if let JsValue::Promise(p) = r {
216
- let result = p.get_result();
217
- println!("promise result:{:?}", result);
218
- println!("poll promise");
219
- ctx.promise_loop_poll();
220
- let result = p.get_result();
221
- println!("promise result:{:?}", result);
222
- }
223
- }
224
- }
225
-
226
-
227
-
228
- ---
229
- File: /embed_js/Cargo.toml
230
- ---
231
-
232
- [package]
233
- name = "embed_js"
234
- version = "0.1.0"
235
- authors = ["ubuntu"]
236
- edition = "2018"
237
-
238
- [[bin]]
239
- name = "embed_js"
240
- path = "src/main.rs"
241
-
242
- [dependencies]
243
- wasmedge_quickjs = "0.2.0"
244
-
245
-
246
-
247
-
248
- ---
249
- File: /embed_js/README.md
250
- ---
251
-
252
-
253
- ## Build
254
-
255
- ```
256
- cargo build --target wasm32-wasi --release
257
- ```
258
-
259
- ## Run
260
-
261
- ```
262
- wasmedge --dir .:. target/wasm32-wasi/release/embed_js.wasm
263
- ```
264
-
265
-
266
-
267
- ---
268
- File: /embed_js_module/src/main.rs
269
- ---
270
-
271
- use wasmedge_quickjs::*;
272
-
273
- fn main() {
274
- let mut ctx = Context::new();
275
-
276
- let code = r#"
277
- import('async_demo.js').then((demo)=>{
278
- return demo.wait_simple_val(1)
279
- })
280
- "#;
281
-
282
- let p = ctx.eval_global_str(code);
283
- println!("before poll:{:?}", p);
284
- if let JsValue::Promise(ref p) = p {
285
- let v = p.get_result();
286
- println!("v = {:?}", v);
287
- }
288
- ctx.promise_loop_poll();
289
- println!("after poll:{:?}", p);
290
- if let JsValue::Promise(ref p) = p {
291
- let v = p.get_result();
292
- println!("v = {:?}", v);
293
- }
294
- }
295
-
296
-
297
-
298
- ---
299
- File: /embed_js_module/async_demo.js
300
- ---
301
-
302
- import * as std from 'std'
303
-
304
- async function simple_val (){
305
- return "abc"
306
- }
307
-
308
- export async function wait_simple_val (a){
309
- let x = await simple_val()
310
- print("wait_simple_val:",a,':',x)
311
- return 12345
312
- }
313
-
314
-
315
-
316
- ---
317
- File: /embed_js_module/Cargo.toml
318
- ---
319
-
320
- [package]
321
- name = "embed_js_module"
322
- version = "0.1.0"
323
- authors = ["ubuntu"]
324
- edition = "2018"
325
-
326
- [[bin]]
327
- name = "embed_js_module"
328
- path = "src/main.rs"
329
-
330
- [dependencies]
331
- wasmedge_quickjs = "0.2.0"
332
-
333
-
334
-
335
-
336
- ---
337
- File: /embed_js_module/README.md
338
- ---
339
-
340
-
341
- ## Build
342
-
343
- ```
344
- cargo build --target wasm32-wasi --release
345
- ```
346
-
347
- ## Run
348
-
349
- ```
350
- wasmedge --dir .:. target/wasm32-wasi/release/embed_js_module.wasm
351
- ```
352
-
353
-
354
-
355
- ---
356
- File: /embed_rust_module/src/main.rs
357
- ---
358
-
359
- mod point {
360
- use wasmedge_quickjs::*;
361
-
362
- #[derive(Debug)]
363
- struct Point(i32, i32);
364
-
365
- struct PointDef;
366
-
367
- impl JsClassDef<Point> for PointDef {
368
- const CLASS_NAME: &'static str = "Point\0";
369
- const CONSTRUCTOR_ARGC: u8 = 2;
370
-
371
- fn constructor(_: &mut Context, argv: &[JsValue]) -> Option<Point> {
372
- println!("rust-> new Point {:?}", argv);
373
- let x = argv.get(0);
374
- let y = argv.get(1);
375
- if let (Some(JsValue::Int(ref x)), Some(JsValue::Int(ref y))) = (x, y) {
376
- Some(Point(*x, *y))
377
- } else {
378
- None
379
- }
380
- }
381
-
382
- fn proto_init(p: &mut JsClassProto<Point, PointDef>) {
383
- struct X;
384
- impl JsClassGetterSetter<Point> for X {
385
- const NAME: &'static str = "x\0";
386
-
387
- fn getter(_: &mut Context, this_val: &mut Point) -> JsValue {
388
- println!("rust-> get x");
389
- this_val.0.into()
390
- }
391
-
392
- fn setter(_: &mut Context, this_val: &mut Point, val: JsValue) {
393
- println!("rust-> set x:{:?}", val);
394
- if let JsValue::Int(x) = val {
395
- this_val.0 = x
396
- }
397
- }
398
- }
399
-
400
- struct Y;
401
- impl JsClassGetterSetter<Point> for Y {
402
- const NAME: &'static str = "y\0";
403
-
404
- fn getter(_: &mut Context, this_val: &mut Point) -> JsValue {
405
- println!("rust-> get y");
406
- this_val.1.into()
407
- }
408
-
409
- fn setter(_: &mut Context, this_val: &mut Point, val: JsValue) {
410
- println!("rust-> set y:{:?}", val);
411
- if let JsValue::Int(y) = val {
412
- this_val.1 = y
413
- }
414
- }
415
- }
416
-
417
- struct FnPrint;
418
- impl JsMethod<Point> for FnPrint {
419
- const NAME: &'static str = "pprint\0";
420
- const LEN: u8 = 0;
421
-
422
- fn call(_: &mut Context, this_val: &mut Point, _argv: &[JsValue]) -> JsValue {
423
- println!("rust-> pprint: {:?}", this_val);
424
- JsValue::Int(1)
425
- }
426
- }
427
-
428
- p.add_getter_setter(X);
429
- p.add_getter_setter(Y);
430
- p.add_function(FnPrint);
431
- }
432
- }
433
-
434
- struct PointModule;
435
- impl ModuleInit for PointModule {
436
- fn init_module(ctx: &mut Context, m: &mut JsModuleDef) {
437
- m.add_export("Point\0", PointDef::class_value(ctx));
438
- }
439
- }
440
-
441
- pub fn init_point_module(ctx: &mut Context) {
442
- ctx.register_class(PointDef);
443
- ctx.register_module("point\0", PointModule, &["Point\0"]);
444
- }
445
- }
446
-
447
- use wasmedge_quickjs::*;
448
- fn main() {
449
- let mut ctx = Context::new();
450
- point::init_point_module(&mut ctx);
451
-
452
- let code = r#"
453
- import('point').then((point)=>{
454
- let p0 = new point.Point(1,2)
455
- print("js->",p0.x,p0.y)
456
- p0.pprint()
457
-
458
- try{
459
- let p = new point.Point()
460
- print("js-> p:",p)
461
- print("js->",p.x,p.y)
462
- p.x=2
463
- p.pprint()
464
- } catch(e) {
465
- print("An error has been caught");
466
- print(e)
467
- }
468
-
469
- })
470
- "#;
471
-
472
- ctx.eval_global_str(code);
473
- ctx.promise_loop_poll();
474
- }
475
-
476
-
477
-
478
- ---
479
- File: /embed_rust_module/Cargo.toml
480
- ---
481
-
482
- [package]
483
- name = "embed_rust_module"
484
- version = "0.1.0"
485
- authors = ["ubuntu"]
486
- edition = "2018"
487
-
488
- [[bin]]
489
- name = "embed_rust_module"
490
- path = "src/main.rs"
491
-
492
- [dependencies]
493
- wasmedge_quickjs = "0.2.0"
494
-
495
-
496
-
497
-
498
- ---
499
- File: /embed_rust_module/README.md
500
- ---
501
-
502
-
503
- ## Build
504
-
505
- ```
506
- cargo build --target wasm32-wasi --release
507
- ```
508
-
509
- ## Run
510
-
511
- ```
512
- wasmedge --dir .:. target/wasm32-wasi/release/embed_rust_module.wasm
513
- ```
514
-
515
-
516
-
517
- ---
518
- File: /embed_use_es6_module/src/main.rs
519
- ---
520
-
521
- use wasmedge_quickjs::*;
522
-
523
- fn main() {
524
- let mut ctx = Context::new();
525
-
526
- let code = r#"
527
- let m = import('es6_module_demo.js')
528
- m
529
- "#;
530
-
531
- let p = ctx.eval_global_str(code);
532
- println!("before poll:{:?}", p);
533
- ctx.promise_loop_poll();
534
- println!("after poll:{:?}", p);
535
- if let JsValue::Promise(ref p) = p {
536
- let m = p.get_result();
537
- println!("m = {:?}", m);
538
- if let JsValue::Object(mod_obj) = m {
539
- let f = mod_obj.get("do_something");
540
- println!("do_something = {:?}", f);
541
- if let JsValue::Function(f) = f {
542
- f.call(&mut [ctx.new_string("hello").into()]);
543
- }
544
- }
545
- }
546
- }
547
-
548
-
549
-
550
- ---
551
- File: /embed_use_es6_module/Cargo.toml
552
- ---
553
-
554
- [package]
555
- name = "embed_use_es6_module"
556
- version = "0.1.0"
557
- edition = "2018"
558
-
559
- # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
560
-
561
- [dependencies]
562
- wasmedge_quickjs = "0.2.0"
563
-
564
-
565
- ---
566
- File: /embed_use_es6_module/es6_module_demo.js
567
- ---
568
-
569
- import * as std from 'std';
570
-
571
- export function do_something(a) {
572
- print('[es6_module_demo.js]=> do_something: a =', a);
573
- return a;
574
- }
575
-
576
-
577
-
578
- ---
579
- File: /host_function/src/main.rs
580
- ---
581
-
582
- mod host_extern {
583
- use wasmedge_quickjs::{Context, JsFn, JsValue};
584
-
585
- #[link(wasm_import_module = "extern")]
586
- extern "C" {
587
- pub fn host_inc(v: i32) -> i32;
588
- }
589
-
590
- pub struct HostIncFn;
591
- impl JsFn for HostIncFn {
592
- fn call(ctx: &mut Context, _this_val: JsValue, argv: &[JsValue]) -> JsValue {
593
- if let Some(JsValue::Int(i)) = argv.get(0) {
594
- unsafe {
595
- let r = host_inc(*i);
596
- r.into()
597
- }
598
- } else {
599
- ctx.throw_type_error("'v' is not a int").into()
600
- }
601
- }
602
- }
603
- }
604
-
605
- use wasmedge_quickjs::*;
606
-
607
- fn main() {
608
- let mut ctx = Context::new();
609
- let f = ctx.new_function::<host_extern::HostIncFn>("host_inc");
610
- ctx.get_global().set("host_inc", f.into());
611
- ctx.eval_global_str("print('js=> host_inc(2)=',host_inc(2))");
612
- }
613
-
614
-
615
-
616
- ---
617
- File: /host_function/wasmedge_c/demo_wasmedge.c
618
- ---
619
-
620
- #include <stdio.h>
621
- #include "wasmedge/wasmedge.h"
622
-
623
- WasmEdge_Result HostInc(void *Data, WasmEdge_MemoryInstanceContext *MemCxt,
624
- const WasmEdge_Value *In, WasmEdge_Value *Out) {
625
- int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
626
- printf("Runtime(c)=> host_inc call : %d\n",Val1 + 1);
627
- Out[0] = WasmEdge_ValueGenI32(Val1 + 1);
628
- return WasmEdge_Result_Success;
629
- }
630
-
631
- // mapping dirs
632
- const char* dirs = ".:..\0";
633
-
634
- int main(int Argc, const char* Argv[]) {
635
- /* Create the configure context and add the WASI support. */
636
- /* This step is not necessary unless you need WASI support. */
637
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
638
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
639
- /* The configure and store context to the VM creation can be NULL. */
640
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
641
- WasmEdge_ImportObjectContext *WasiObject = WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_Wasi);
642
- WasmEdge_ImportObjectInitWASI(WasiObject,Argv+1,Argc-1,NULL,0, &dirs,1);
643
-
644
-
645
- /* Create the import object. */
646
- WasmEdge_String ExportName = WasmEdge_StringCreateByCString("extern");
647
- WasmEdge_ImportObjectContext *ImpObj = WasmEdge_ImportObjectCreate(ExportName);
648
- enum WasmEdge_ValType ParamList[1] = { WasmEdge_ValType_I32 };
649
- enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
650
- WasmEdge_FunctionTypeContext *FuncType = WasmEdge_FunctionTypeCreate(ParamList, 1, ReturnList, 1);
651
- WasmEdge_FunctionInstanceContext *HostFunc = WasmEdge_FunctionInstanceCreate(FuncType, HostInc, NULL, 0);
652
- WasmEdge_FunctionTypeDelete(FuncType);
653
- WasmEdge_String HostFuncName = WasmEdge_StringCreateByCString("host_inc");
654
- WasmEdge_ImportObjectAddFunction(ImpObj, HostFuncName, HostFunc);
655
- WasmEdge_StringDelete(HostFuncName);
656
-
657
- WasmEdge_VMRegisterModuleFromImport(VMCxt, ImpObj);
658
-
659
-
660
- /* The parameters and returns arrays. */
661
- WasmEdge_Value Params[0];
662
- WasmEdge_Value Returns[0];
663
- /* Function name. */
664
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("_start");
665
- /* Run the WASM function from file. */
666
- WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(VMCxt, Argv[1], FuncName, Params, 0, Returns, 0);
667
-
668
- if (WasmEdge_ResultOK(Res)) {
669
- printf("\nRuntime(c)=> OK\n");
670
- } else {
671
- printf("\nRuntime(c)=> Error message: %s\n", WasmEdge_ResultGetMessage(Res));
672
- }
673
-
674
- /* Resources deallocations. */
675
- WasmEdge_VMDelete(VMCxt);
676
- WasmEdge_ConfigureDelete(ConfCxt);
677
- WasmEdge_StringDelete(FuncName);
678
- return 0;
679
- }
680
-
681
-
682
- ---
683
- File: /host_function/Cargo.toml
684
- ---
685
-
686
- [package]
687
- name = "host_function"
688
- version = "0.1.0"
689
- authors = ["csh <458761603@qq.com>"]
690
- edition = "2018"
691
-
692
- # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
693
-
694
- [dependencies]
695
- wasmedge_quickjs = "0.2.0"
696
-
697
-
698
-
699
- ---
700
- File: /host_function/README.md
701
- ---
702
-
703
- # A wasi quickjs binding for rust
704
- this example show how to import a custom host function into quickjs.
705
-
706
- # Build
707
-
708
- ```shell
709
- #build wasm
710
- $ cargo build --target wasm32-wasi --release
711
-
712
- #build custom webassembly Runtime
713
- $ cd wasmedge_c
714
-
715
- #build a custom Runtime
716
- wasmedge_c/$ gcc demo_wasmedge.c -lwasmedge_c -o demo_wasmedge
717
- ```
718
-
719
- # Run
720
-
721
- ```shell
722
- wasmedge_c/$ export LD_LIBRARY_PATH=.
723
-
724
- wasmedge_c/$ ./demo_wasmedge ../target/wasm32-wasi/release/host_function.wasm
725
- Runtime(c)=> host_inc call : 3
726
- js=> host_inc(2)= 3
727
-
728
- Runtime(c)=> OK
729
- wasmedge_c/$
730
- ```
731
-
732
-
733
-
734
- ---
735
- File: /js_extend.rs
736
- ---
737
-
738
- use wasmedge_quickjs::js_class;
739
- use wasmedge_quickjs::{
740
- AsObject, Context, ExtendsJsClassDef, JsClassDef, JsClassField, JsClassMethod, JsClassTool,
741
- JsObject, JsValue, Runtime,
742
- };
743
-
744
- #[derive(Debug)]
745
- struct ClassA(i32);
746
-
747
- impl ClassA {
748
- pub fn get_val(&self, _ctx: &mut Context) -> JsValue {
749
- JsValue::Int(self.0)
750
- }
751
-
752
- pub fn inc(
753
- &mut self,
754
- _this_obj: &mut JsObject,
755
- _ctx: &mut Context,
756
- _argv: &[JsValue],
757
- ) -> JsValue {
758
- self.0 += 1;
759
- JsValue::Int(self.0)
760
- }
761
- }
762
-
763
- impl JsClassDef for ClassA {
764
- type RefType = ClassA;
765
-
766
- const CLASS_NAME: &'static str = "ClassA";
767
-
768
- const CONSTRUCTOR_ARGC: u8 = 1;
769
-
770
- const FIELDS: &'static [JsClassField<Self::RefType>] = &[("val", ClassA::get_val, None)];
771
-
772
- const METHODS: &'static [JsClassMethod<Self::RefType>] = &[("inc", 0, ClassA::inc)];
773
-
774
- unsafe fn mut_class_id_ptr() -> &'static mut u32 {
775
- static mut CLASS_ID: u32 = 0;
776
- &mut CLASS_ID
777
- }
778
-
779
- fn constructor_fn(
780
- _ctx: &mut wasmedge_quickjs::Context,
781
- argv: &[wasmedge_quickjs::JsValue],
782
- ) -> Result<Self::RefType, wasmedge_quickjs::JsValue> {
783
- match argv.get(0) {
784
- Some(JsValue::Int(v)) => Ok(ClassA(*v)),
785
- _ => Ok(ClassA(0)),
786
- }
787
- }
788
- }
789
-
790
- #[derive(Debug)]
791
- struct ClassB(ClassA, i32);
792
-
793
- impl AsRef<ClassA> for ClassB {
794
- fn as_ref(&self) -> &ClassA {
795
- &self.0
796
- }
797
- }
798
-
799
- impl AsMut<ClassA> for ClassB {
800
- fn as_mut(&mut self) -> &mut ClassA {
801
- &mut self.0
802
- }
803
- }
804
-
805
- impl ClassB {
806
- pub fn get_val_b(&self, _ctx: &mut Context) -> JsValue {
807
- JsValue::Int(self.1)
808
- }
809
-
810
- pub fn inc_b(
811
- &mut self,
812
- _this_obj: &mut JsObject,
813
- _ctx: &mut Context,
814
- _argv: &[JsValue],
815
- ) -> JsValue {
816
- self.1 += 1;
817
- JsValue::Int(self.1)
818
- }
819
-
820
- pub fn display(
821
- &mut self,
822
- _this_obj: &mut JsObject,
823
- _ctx: &mut Context,
824
- _argv: &[JsValue],
825
- ) -> JsValue {
826
- println!("display=> {:?}", self);
827
- JsValue::UnDefined
828
- }
829
- }
830
-
831
- impl ExtendsJsClassDef for ClassB {
832
- type RefType = ClassB;
833
-
834
- type BaseDef = ClassA;
835
-
836
- const EXT_CLASS_NAME: &'static str = "ClassB";
837
-
838
- const CONSTRUCTOR_ARGC: u8 = 1;
839
-
840
- const FIELDS: &'static [JsClassField<Self::RefType>] = &[("val_b", ClassB::get_val_b, None)];
841
-
842
- const METHODS: &'static [JsClassMethod<Self::RefType>] =
843
- &[("inc_b", 0, ClassB::inc_b), ("display", 0, ClassB::display)];
844
-
845
- unsafe fn mut_class_id_ptr() -> &'static mut u32 {
846
- static mut CLASS_ID: u32 = 0;
847
- &mut CLASS_ID
848
- }
849
-
850
- fn constructor_fn(
851
- ctx: &mut wasmedge_quickjs::Context,
852
- argv: &[JsValue],
853
- ) -> Result<Self::RefType, JsValue> {
854
- let a = ClassA::constructor_fn(ctx, argv)?;
855
- Ok(ClassB(a, 1))
856
- }
857
- }
858
-
859
- fn main() {
860
- let mut rt = Runtime::new();
861
- rt.run_with_context(|ctx| {
862
- let a_ctor = js_class::register_class::<ClassA>(ctx);
863
- let b_ctor = js_class::register_class::<ClassB>(ctx);
864
-
865
- let a_proto = ClassA::proto(ctx);
866
- let b_proto = ClassB::proto(ctx);
867
-
868
- js_class::class_extends(ctx, b_proto, a_proto);
869
-
870
- let mut global = ctx.get_global();
871
- global.set("ClassA", a_ctor);
872
- global.set("ClassB", b_ctor);
873
-
874
- let code = r#"
875
- let a = new ClassA(1)
876
- print('a.val =',a.val)
877
- print('a.inc() =',a.inc())
878
- print('a.val =',a.val)
879
- print()
880
-
881
- let b = new ClassB()
882
- print('b.val =',b.val)
883
- print('b.inc() =',b.inc())
884
- print('b.val =',b.val)
885
- print()
886
-
887
- print('b.val_b =',b.val_b)
888
- print('b.inc_b() =',b.inc_b())
889
- print('b.val_b =',b.val_b)
890
- print()
891
-
892
- b.display()
893
- print()
894
-
895
- print('b instanceof ClassA =',b instanceof ClassA)
896
- "#;
897
- ctx.eval_global_str(code.to_string());
898
- })
899
- }
900
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
output/local_repo/docs/quick_js_js.txt DELETED
The diff for this file is too large to render. See raw diff
 
output/local_repo/docs/wasmedge_docs.txt DELETED
The diff for this file is too large to render. See raw diff