1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include "aos_fs.h"
6 #include "aos_system.h"
7 #include "amp_config.h"
8 #include "amp_defines.h"
9 #include "amp_task.h"
10 #include "board_mgr.h"
11 #include "be_inl.h"
12 #include "cJSON.h"
13 #include "startup/app_entry.h"
14
15 #define MOD_STR "APPENTRY"
16
17 app_options_t app_options;
18
19 /* App(Object options) entry */
native_app_entry(duk_context * ctx)20 static duk_ret_t native_app_entry(duk_context *ctx)
21 {
22 int i;
23
24 /* check paramters */
25 if (!duk_is_object(ctx, 0))
26 {
27 // amp_warn("parameter must be object\n");
28 duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "parameter must be object");
29 return duk_throw(ctx);
30 }
31
32 memset(&app_options, 0, sizeof(app_options_t));
33
34 /* get options object */
35 duk_dup(ctx, -1);
36 app_options.object = be_ref(ctx);
37
38 /* find globalData */
39 if (duk_get_prop_string(ctx, 0, "globalData"))
40 {
41 if (!duk_is_object(ctx, -1))
42 {
43 duk_pop(ctx);
44 duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "onLaunch must be function");
45 return duk_throw(ctx);
46 }
47 amp_debug(MOD_STR, "find globalData\n");
48 duk_dup(ctx, -1);
49 app_options.global_data = be_ref(ctx);
50 duk_pop(ctx);
51 }
52
53 /* find onLaunch() */
54 if (duk_get_prop_string(ctx, 0, "onLaunch"))
55 {
56 if (!duk_is_function(ctx, -1))
57 {
58 duk_pop(ctx);
59 duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "onLaunch must be function");
60 return duk_throw(ctx);
61 }
62 amp_debug(MOD_STR, "find onLaunch()\n");
63 duk_dup(ctx, -1);
64 app_options.on_launch = be_ref(ctx);
65 duk_pop(ctx);
66 }
67
68 /* find onError() */
69 if (duk_get_prop_string(ctx, 0, "onError"))
70 {
71 if (!duk_is_function(ctx, -1))
72 {
73 duk_pop(ctx);
74 duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "onError must be function");
75 return duk_throw(ctx);
76 }
77 amp_debug(MOD_STR, "find onError()\n");
78 duk_dup(ctx, -1);
79 app_options.on_error = be_ref(ctx);
80 duk_pop(ctx);
81 }
82
83 /* find onError() */
84 if (duk_get_prop_string(ctx, 0, "onExit"))
85 {
86 if (!duk_is_function(ctx, -1))
87 {
88 duk_pop(ctx);
89 duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "onExit must be function");
90 return duk_throw(ctx);
91 }
92 amp_debug(MOD_STR, "find onExit()\n");
93 duk_dup(ctx, -1);
94 app_options.on_exit = be_ref(ctx);
95 duk_pop(ctx);
96 }
97
98 amp_task_schedule_call(app_entry, NULL);
99 return 1; /* one return value */
100 }
101
app_entry(void * data)102 void app_entry(void *data)
103 {
104 int i = 0;
105
106 duk_context *ctx = be_get_context();
107 /* onLaunch hook */
108 be_push_ref(ctx, app_options.on_launch);
109
110 duk_push_object(ctx);
111
112 if (duk_pcall(ctx, 1) != DUK_EXEC_SUCCESS) {
113 amp_console("%s", duk_safe_to_stacktrace(ctx, -1));
114 }
115 duk_pop(ctx);
116 duk_gc(ctx, 0);
117 /* onExit hook */
118 // be_push_ref(ctx, app_options.on_exit);
119
120 // duk_push_object(ctx);
121
122 // duk_pcall(ctx, 1);
123 // duk_pop(ctx);
124 }
125
app_entry_register(void)126 void app_entry_register(void)
127 {
128 duk_context *ctx = be_get_context();
129 duk_push_object(ctx);
130
131 /* App({}) */
132 duk_push_c_function(ctx, native_app_entry, 1);
133 duk_put_global_string(ctx, "App");
134 }
135