1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #include "amp_config.h"
6 #include "amp_defines.h"
7 #include "und.h"
8 #include "be_inl.h"
9 
10 #define MOD_STR "UND"
11 /*****************************************************************************
12  * Function:    native_fs_issupport
13  * Description: js native addon for FILE.issupport()
14  *            check if the js FILE object is support
15  * Called by:   js api
16  * Input:       none
17  * Output:      1 :support ,0 :not support
18  *****************************************************************************/
native_und_start(duk_context * ctx)19 static duk_ret_t native_und_start(duk_context *ctx)
20 {
21     int32_t ret = -1;
22 
23     ret = und_init();
24     if (ret != 0) {
25         amp_warn(MOD_STR, "und init failed");
26         goto out;
27     }
28 
29 out:
30     duk_push_int(ctx, ret);
31     return 1;
32 }
33 
34 /*****************************************************************************
35  * Function:    native_fs_read
36  * Description: js native addon for FILE.read(filepath)
37  *            read the file content
38  * Called by:   js api
39  * Input:       filepath : string
40  * Output:      a String object which the file content is
41  *****************************************************************************/
native_und_update(duk_context * ctx)42 static duk_ret_t native_und_update(duk_context *ctx)
43 {
44     int32_t ret = -1;
45     int32_t cap_idx;
46     int32_t reason_code;
47 
48     if (!duk_is_number(ctx, 0) || !duk_is_number(ctx, 1)) {
49         amp_warn(MOD_STR, "invalid parameter\n");
50         goto out;
51     }
52 
53     cap_idx = duk_get_int(ctx, 0);
54     reason_code = duk_get_int(ctx, 1);
55 
56     ret = und_update_statis(cap_idx, reason_code);
57     if (ret != 0) {
58         amp_warn(MOD_STR, "und update statis failed");
59         goto out;
60     }
61 
62 out:
63     duk_push_int(ctx, ret);
64     return 1;
65 }
66 
67 /*****************************************************************************
68  * Function:    native_fs_delete
69  * Description: js native addon for FILE.delete(filepath)
70  *            delete the file
71  * Called by:   js api
72  * Input:       filepath : string
73  * Output:      0 delete ok ;other delete fail
74  *****************************************************************************/
native_und_stop(duk_context * ctx)75 static duk_ret_t native_und_stop(duk_context *ctx)
76 {
77     int32_t ret = -1;
78 
79     ret = und_deinit();
80     if (ret != 0) {
81         amp_warn(MOD_STR, "und deinit failed");
82         goto out;
83     }
84 
85 out:
86     duk_push_int(ctx, ret);
87     return 1;
88 }
89 
module_und_register(void)90 void module_und_register(void)
91 {
92     duk_context *ctx = be_get_context();
93 
94     duk_push_object(ctx);
95 
96     AMP_ADD_FUNCTION("start",  native_und_start, 0);
97     AMP_ADD_FUNCTION("update", native_und_update, 2);
98     AMP_ADD_FUNCTION("stop",   native_und_stop, 0);
99 
100     duk_put_global_string(ctx, "UND");
101 }
102