1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #include "amp_config.h"
6 #include "aos/kv.h"
7 #include "amp_defines.h"
8 #include "quickjs.h"
9 #include "quickjs_addon_common.h"
10 #include "aos_system.h"
11
12 #define MOD_STR "KV"
13 #define KV_BUFFER_MAX_LEN 256
14
15 static JSClassID js_kv_class_id;
16
native_kv_setStorage(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)17 static JSValue native_kv_setStorage(JSContext *ctx, JSValueConst this_val,
18 int argc, JSValueConst *argv)
19 {
20 int32_t ret = -1;
21 int32_t value_len = 0;
22 const char *key = NULL;
23 const char *value = NULL;
24
25 key = JS_ToCString(ctx, argv[0]);
26 if (key == NULL) {
27 amp_error(MOD_STR, "get kv key fail!");
28 goto out;
29 }
30
31 value = JS_ToCString(ctx, argv[1]);
32 if (value == NULL) {
33 amp_error(MOD_STR, "get kv value fail!");
34 goto out;
35 }
36
37 value_len = strlen(value);
38
39 ret = aos_kv_set(key, value, value_len, 1);
40 if (ret != 0) {
41 amp_warn(MOD_STR, "kv set storage failed");
42 }
43
44 out:
45 if (key != NULL) {
46 JS_FreeCString(ctx, key);
47 }
48 if (value != NULL) {
49 JS_FreeCString(ctx, value);
50 }
51 return JS_NewInt32(ctx, ret);
52 }
53
native_kv_getStorage(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)54 static JSValue native_kv_getStorage(JSContext *ctx, JSValueConst this_val,
55 int argc, JSValueConst *argv)
56 {
57 int32_t ret = -1;
58 int32_t value_len = KV_BUFFER_MAX_LEN;
59 const char *key = NULL;
60 char *value = NULL;
61 JSValue obj = JS_UNDEFINED;
62
63 key = JS_ToCString(ctx, argv[0]);
64 if (key == NULL) {
65 amp_error(MOD_STR, "get kv key fail!");
66 goto out;
67 }
68
69 value = (char *)aos_malloc(KV_BUFFER_MAX_LEN);
70 if (value == NULL) {
71 amp_warn(MOD_STR, "allocate memory failed\n");
72 goto out;
73 }
74
75 ret = aos_kv_get(key, value, &value_len);
76 if (ret != 0) {
77 amp_warn(MOD_STR, "kv get storage failed");
78 goto out;
79 }
80
81 obj = JS_NewStringLen(ctx, value, value_len);
82
83 out:
84 if (key != NULL) {
85 JS_FreeCString(ctx, key);
86 }
87 if (value != NULL) {
88 aos_free(value);
89 }
90 return obj;
91 }
92
native_kv_removeStorage(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)93 static JSValue native_kv_removeStorage(JSContext *ctx, JSValueConst this_val,
94 int argc, JSValueConst *argv)
95 {
96 int32_t ret = -1;
97 const char *key = NULL;
98
99 key = JS_ToCString(ctx, argv[0]);
100 if (key == NULL) {
101 amp_error(MOD_STR, "get kv key fail!");
102 goto out;
103 }
104
105 ret = aos_kv_del(key);
106 if (ret != 0) {
107 amp_warn(MOD_STR, "kv delete item failed");
108 }
109
110 out:
111 if (key != NULL) {
112 JS_FreeCString(ctx, key);
113 }
114 return JS_NewInt32(ctx, ret);
115 }
116
117 static JSClassDef js_kv_class = {
118 "KV",
119 };
120
121 static const JSCFunctionListEntry js_kv_funcs[] = {
122 JS_CFUNC_DEF("setStorageSync", 2, native_kv_setStorage),
123 JS_CFUNC_DEF("getStorageSync", 1, native_kv_getStorage),
124 JS_CFUNC_DEF("removeStorageSync", 1, native_kv_removeStorage),
125 };
126
js_kv_init(JSContext * ctx,JSModuleDef * m)127 static int js_kv_init(JSContext *ctx, JSModuleDef *m)
128 {
129 JSValue proto;
130
131 JS_NewClassID(&js_kv_class_id);
132
133 JS_NewClass(JS_GetRuntime(ctx), js_kv_class_id, &js_kv_class);
134 proto = JS_NewObject(ctx);
135 JS_SetPropertyFunctionList(ctx, proto, js_kv_funcs,
136 countof(js_kv_funcs));
137 JS_SetClassProto(ctx, js_kv_class_id, proto);
138
139 return JS_SetModuleExportList(ctx, m, js_kv_funcs,
140 countof(js_kv_funcs));
141 }
142
js_init_module_kv(JSContext * ctx,const char * module_name)143 JSModuleDef *js_init_module_kv(JSContext *ctx, const char *module_name)
144 {
145 JSModuleDef *m;
146 m = JS_NewCModule(ctx, module_name, js_kv_init);
147 if (!m)
148 return NULL;
149 JS_AddModuleExportList(ctx, m, js_kv_funcs, countof(js_kv_funcs));
150 return m;
151 }
152
module_kv_register(void)153 void module_kv_register(void)
154 {
155 amp_debug(MOD_STR, "module_kv_register");
156 JSContext *ctx = js_get_context();
157 aos_printf("module kv register\n");
158 js_init_module_kv(ctx, "kv");
159 }
160