-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSPlugin.c
More file actions
181 lines (151 loc) · 6.3 KB
/
Copy pathJSPlugin.c
File metadata and controls
181 lines (151 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "duktape.h"
#define SCRIPTING_DIRECTORY "js"
#define SCRIPTING_ARGS duk_context* ctx
#define SCRIPTING_CALL ctx
#define SCRIPTING_RESULT duk_ret_t
#define SCRIPTING_FUNC const duk_function_list_entry
#define Scripting_DeclareFunc(name, func, num_args) { name, func, num_args }
#define Scripting_ReturnVoid() return 1;
#define Scripting_ReturnInt(value) duk_push_int(ctx, value); return 1;
#define Scripting_ReturnBool(value) duk_push_boolean(ctx, value); return 1;
#define Scripting_ReturnStr(buffer, len) duk_push_lstring(ctx, buffer, len); return 1;
#define Scripting_ReturnPtr(value) duk_push_pointer(ctx, value); return 1;
#define Scripting_ReturnNum(value) duk_push_number(ctx, value); return 1;
#include "../../Scripting.h"
/*########################################################################################################################*
*--------------------------------------------------------Backend----------------------------------------------------------*
*#########################################################################################################################*/
static cc_string Scripting_GetStr(SCRIPTING_ARGS, int arg) {
cc_string str;
duk_size_t len;
str.buffer = duk_to_lstring(ctx, arg, &len);
str.length = len;
str.capacity = 0;
return str;
}
static int Scripting_GetInt(SCRIPTING_ARGS, int arg) {
return duk_to_int(ctx, arg);
}
static double Scripting_GetNum(SCRIPTING_ARGS, int arg) {
return duk_to_number(ctx, arg);
}
static sc_buffer Scripting_GetBuf(SCRIPTING_ARGS, int arg) {
sc_buffer buffer;
duk_size_t size;
buffer.data = duk_to_buffer(ctx, arg, &size);
buffer.len = size;
return buffer;
}
static void Scripting_FreeStr(cc_string* str) {
// no need to manually free
}
static void Scripting_FreeBuf(sc_buffer* buf) {
// no need to manually free
}
/*########################################################################################################################*
*--------------------------------------------------------Base API---------------------------------------------------------*
*#########################################################################################################################*/
struct JSPlugin;
typedef struct JSPlugin { duk_context* ctx; struct JSPlugin* next; } JSPlugin;
static JSPlugin* pluginsHead;
static cc_string Backend_GetError(SCRIPTING_ARGS) {
return String_FromReadonly(duk_safe_to_string(ctx, -1));
}
// macro to avoid verbose code duplication
#define JSPlugin_RaiseCommonBegin \
JSPlugin* plugin = pluginsHead;\
while (plugin) {\
duk_context* ctx = plugin->ctx;\
duk_push_global_object(ctx);\
duk_get_prop_string(ctx, -1, groupName);\
duk_get_prop_string(ctx, -1, funcName);\
if (duk_is_function(ctx, -1)) { \
#define JSPlugin_RaiseCommonEnd \
} else {\
duk_pop_n(ctx, 1); /* pop function manually */ \
}\
duk_pop_n(ctx, 2); /* pop object name and global */ \
plugin = plugin->next;\
}
static void Backend_RaiseVoid(const char* groupName, const char* funcName) {
JSPlugin_RaiseCommonBegin
duk_int_t ret = duk_pcall(ctx, 0);
if (ret != 0) Scripting_LogError(ctx, "running callback", groupName, funcName);
JSPlugin_RaiseCommonEnd
}
static void Backend_RaiseChat(const char* groupName, const char* funcName, const cc_string* msg, int msgType) {
JSPlugin_RaiseCommonBegin
duk_push_lstring(ctx, msg->buffer, msg->length);
duk_push_int(ctx, msgType);
duk_int_t ret = duk_pcall(ctx, 2);
if (ret != 0) Scripting_LogError(ctx, "running callback", groupName, funcName);
JSPlugin_RaiseCommonEnd
}
/*########################################################################################################################*
*-------------------------------------------------Plugin implementation---------------------------------------------------*
*#########################################################################################################################*/
static void JSPlugin_RegisterModule(duk_context* ctx, const char* name, const duk_function_list_entry* funcs) {
duk_push_global_object(ctx);
duk_push_object(ctx);
duk_put_function_list(ctx, -1, funcs);
duk_put_prop_string(ctx, -2, name);
duk_pop(ctx);
}
static void JSPlugin_Register(duk_context* ctx) {
JSPlugin_RegisterModule(ctx, "block", blockFuncs);
JSPlugin_RegisterModule(ctx, "camera", cameraFuncs);
JSPlugin_RegisterModule(ctx, "env", envFuncs);
JSPlugin_RegisterModule(ctx, "chat", chatFuncs);
JSPlugin_RegisterModule(ctx, "game", gameFuncs);
JSPlugin_RegisterModule(ctx, "inventory", inventoryFuncs);
JSPlugin_RegisterModule(ctx, "player", playerFuncs);
JSPlugin_RegisterModule(ctx, "server", serverFuncs);
JSPlugin_RegisterModule(ctx, "tablist", tablistFuncs);
JSPlugin_RegisterModule(ctx, "world", worldFuncs);
JSPlugin_RegisterModule(ctx, "window", windowFuncs);
}
static duk_context* JSPlugin_New(void) {
duk_context* ctx = duk_create_heap_default();
JSPlugin_Register(ctx);
return ctx;
}
static void Backend_Load(const cc_string* path, void* obj, int is_dir) {
static cc_string ext = String_FromConst(".js");
if (is_dir || !String_CaselessEnds(path, &ext)) return;
cc_result res;
sc_buffer mem;
// TODO: What's error checking anyways?
// TODO: leaking memory here
res = Scripting_LoadFile(path, &mem);
if (res) return;
duk_context* ctx = JSPlugin_New();
duk_push_lstring(ctx, mem.data, mem.len);
if (duk_peval(ctx) != 0) {
Scripting_LogError(ctx, "executing script", path, NULL);
duk_destroy_heap(ctx); return;
}
// no need to bother freeing
JSPlugin* plugin = Mem_Alloc(1, sizeof(JSPlugin), "js plugin");
plugin->ctx = ctx;
plugin->next = pluginsHead;
pluginsHead = plugin;
}
static void Backend_ExecScript(const cc_string* script) {
duk_context* ctx = JSPlugin_New();
duk_push_lstring(ctx, script->buffer, script->length);
if (duk_peval(ctx) != 0) {
Scripting_LogError(ctx, "loading script", script, NULL);
}
// TODO: Compile and call
duk_destroy_heap(ctx);
}
static struct ChatCommand JSPlugin_Cmd = {
"js", Scripting_Handle, false,
{
"&a/client js [script]",
"&eExecutes the input text as a JavaScript script",
}
};
static void Backend_Init(void) {
Commands_Register(&JSPlugin_Cmd);
}