This repository was archived by the owner on Dec 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
128 lines (94 loc) · 3.43 KB
/
main.c
File metadata and controls
128 lines (94 loc) · 3.43 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "quickJsEngine.h"
#ifdef QUICKJSENGINE_STANDALONE
//region Sample functions
static void quickFreePointer(JSRuntime *rt, void *opaque, void *ptr) {
free(ptr);
}
static JSValue js_print(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
int i;
const char *str;
for (i = 0; i < argc; i++) {
str = JS_ToCString(ctx, argv[i]);
if (!str) return JS_EXCEPTION;
DEBUG_PRINT(str);
JS_FreeCString(ctx, str);
}
return JS_UNDEFINED;
}
static JSValue js_stringToArrayBuffer(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
const char* str = JS_ToCString(ctx, argv[0]);
if (!str) return JS_EXCEPTION;
char* buffer = strdup(str);
JS_FreeCString(ctx, str);
return JS_NewArrayBuffer(ctx, (uint8_t*)buffer, strlen(buffer), quickFreePointer, NULL, false);
}
static JSValue js_arrayBufferToString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
JSValue arg0 = argv[0];
size_t size;
uint8_t* buffer = JS_GetArrayBuffer(ctx, &size, arg0);
return JS_NewString(ctx, (const char*) buffer);
}
static JSValue js_receiveFunction(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
JSValue arg0 = argv[0];
if (JS_IsFunction(ctx, arg0)) {
return JS_Call(ctx, arg0, JS_UNDEFINED, 0, NULL);
}
return JS_UNDEFINED;
}
static JSValue js_test(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
const char * str = strdup("mon errreur");
s_quick_ctx* pCtx = quickjs_callParamsToAnyValue(ctx, argc, argv);
s_quick_anyValue err;
err.valueType = AnyValueTypeError;
err.voidPtr = (void*)str;
err.size = strlen(str);
err.mustFree = true;
return quickjs_processExternalFunctionCallResult(pCtx, err);
}
//endregion
char* readFile(const char* filePath) {
char *source = NULL;
FILE *fp = fopen(filePath, "r");
if (fp != NULL) {
if (fseek(fp, 0L, SEEK_END) == 0) {
long bufsize = ftell(fp);
if (bufsize == -1) { fclose(fp); return NULL; }
source = malloc(sizeof(char) * (bufsize + 1));
if (fseek(fp, 0L, SEEK_SET) != 0) { fclose(fp); return NULL; }
size_t newLen = fread(source, sizeof(char), bufsize, fp);
if (ferror(fp) != 0) {
fclose(fp); return NULL;
} else {
source[newLen++] = '\0';
}
}
fclose(fp);
}
return source;
}
int main(int argc, char **argv)
{
quickjs_initialize();
const char* scriptPath = "index.js";
char* scriptContent = readFile("index.js");
if (scriptContent==NULL) return 1;
s_quick_ctx* pCtx = quickjs_createContext(NULL);
quickjs_bindFunction(pCtx, "js_print", 1, js_print);
quickjs_bindFunction(pCtx, "js_stringToArrayBuffer", 1, js_stringToArrayBuffer);
quickjs_bindFunction(pCtx, "js_arrayBufferToString", 1, js_arrayBufferToString);
quickjs_bindFunction(pCtx, "js_receiveFunction", 1, js_receiveFunction);
quickjs_bindFunction(pCtx, "js_test", 1, js_test);
s_quick_error* err = quickjs_executeScript(pCtx, scriptContent, scriptPath);
if (err!=NULL) {
DEBUG_PRINT("ERROR: ");
DEBUG_PRINT_KeepLine(err->errorTitle);
DEBUG_PRINT_KeepLine(err->errorStackTrace);
}
free(scriptContent);
return 0;
}
#endif // QUICKJSENGINE_STANDALONE