1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4 #ifdef AMP_ADVANCED_ADDON_UI
5
6 #include "aos_fs.h"
7 #include "aos_system.h"
8 #include "amp_config.h"
9 #include "amp_defines.h"
10 #include "amp_task.h"
11 #include "board_mgr.h"
12 #include "quickjs.h"
13 #include "cJSON.h"
14 #include "app_entry.h"
15 #include "quickjs_addon_common.h"
16
17 #define MOD_STR "APPENTRY"
18
19 app_options_t app_options;
20
21 static JSClassID js_app_entry_class_id;
22
23 /* App(Object options) entry */
native_app_entry(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)24 static JSValue native_app_entry(JSContext *ctx, JSValueConst this_val,
25 int argc, JSValueConst *argv)
26 {
27 int i;
28
29 JSValue irq_cb = argv[0];
30
31 /* check paramters */
32 if (!JS_IsObject(irq_cb))
33 {
34 return JS_ThrowTypeError(ctx, "not a object");
35 }
36
37 memset(&app_options, 0, sizeof(app_options_t));
38
39 /* get options object */
40 app_options.object = JS_DupValue(ctx, irq_cb);
41
42 /* find globalData */
43 if (JS_GetPropertyStr(ctx, argv[0], "globalData"))
44 {
45 amp_debug(MOD_STR, "find app#globalData()");
46 app_options.global_data = JS_DupValue(ctx, irq_cb);
47 }
48
49 /* find onLaunch() */
50 if (JS_GetPropertyStr(ctx, argv[0], "onLaunch"))
51 {
52 amp_debug(MOD_STR, "find app#onLaunch()");
53 app_options.on_launch = JS_DupValue(ctx, irq_cb);
54 }
55
56 /* find onError() */
57 if (JS_GetPropertyStr(ctx, argv[0], "onError"))
58 {
59 amp_debug(MOD_STR, "find app#onError()");
60 app_options.on_error = JS_DupValue(ctx, irq_cb);
61 }
62
63 /* find onExit() */
64 if (JS_GetPropertyStr(ctx, argv[0], "onExit"))
65 {
66 amp_debug(MOD_STR, "find app#onExit()");
67 app_options.on_exit = JS_DupValue(ctx, irq_cb);
68 }
69
70 amp_task_schedule_call(app_entry, NULL);
71 return 1; /* one return value */
72 }
73
app_entry(void * data)74 void app_entry(void *data)
75 {
76 JSContext *ctx = js_get_context();
77 /* onLaunch hook */
78 uint32_t value = 0;
79 JSValue val = JS_Call(ctx, app_options.on_launch, JS_UNDEFINED, 1, &value);
80 JS_FreeValue(ctx, val);
81 }
82
83
84 static JSClassDef js_app_entry_class = {
85 "APPENTRY",
86 };
87
js_app_entry_init(JSContext * ctx)88 static int js_app_entry_init(JSContext *ctx)
89 {
90 JSValue proto;
91
92 JS_NewClassID(&js_app_entry_class_id);
93
94 JS_NewClass(JS_GetRuntime(ctx), js_app_entry_class_id, &js_app_entry_class);
95 proto = JS_NewObject(ctx);
96 JS_SetClassProto(ctx, js_app_entry_class_id, proto);
97
98 return;
99 }
100
app_entry_register(void)101 void app_entry_register(void)
102 {
103 amp_debug(MOD_STR, "module_app_entry_register");
104 JSContext *ctx = js_get_context();
105 aos_printf("module app_entry register\n");
106
107 js_app_entry_init(ctx);
108
109 QUICKJS_GLOBAL_FUNC("App", native_app_entry);
110 }
111
112 #endif