-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.h
83 lines (77 loc) · 2.79 KB
/
console.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#ifndef QJS_INCLUDED
#define QJS_INCLUDED
#include <quickjs/quickjs.h>
#include <quickjs/quickjs-libc.h>
#endif
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSValue js_console_log(JSContext *ctx,JSValueConst self,int argc,JSValueConst *args) {
if ( argc > 1 ) {
for ( int i = 0; i <= argc - 1; i++) {
printf("%s",JS_ToCString(ctx, args[i]));
}
printf("\n");
}
else {
printf("%s\n",JS_ToCString(ctx, args[0]));
}
return JS_NewInt32(ctx, argc);
}
static JSValue js_console_assert(JSContext *ctx,JSValueConst self,int argc,JSValueConst *args) {
if ( argc > 1 ) {
bool success = JS_ToBool(ctx, args[0]);
if ( !success ) {
if ( !JS_IsFunction(ctx,args[1]) ) printf("%s\n",JS_ToCString(ctx, args[1]));
else {
return JS_Call(ctx,args[1],self,argc,args);
}
}
}
else {
return JS_FALSE;
}
return JS_NULL;
}
static JSValue js_console_print(JSContext *ctx,JSValueConst self,int argc,JSValueConst *args) {
if ( argc > 1 ) {
for ( int i = 0; i <= argc - 1; i++) {
printf("%s",JS_ToCString(ctx, args[i]));
}
}
else {
printf("%s",JS_ToCString(ctx, args[0]));
}
return JS_NewInt32(ctx, argc);
}
static JSValue js_console_getpass(JSContext *ctx,JSValueConst self,int argc,JSValueConst *args) {
char *prompt = (char*) malloc(200 * sizeof(char) );
char *data = (char*)malloc(200 * sizeof(char) );
if ( argc < 1) strcpy(prompt,"_");
else strcpy(prompt,JS_ToCString(ctx, args[0]));
data = getpass(prompt);
return JS_NewString(ctx,data);
}
static JSValue js_console_prompt(JSContext *ctx,JSValueConst self,int argc,JSValueConst *args) {
char *prompt = (char*) malloc(200 * sizeof(char));
char *data = (char*) malloc(200 * sizeof(char));
if ( argc < 1 ) strcpy(prompt,"");
else strcpy(prompt,JS_ToCString(ctx, args[0]));
printf("%s",prompt);
fgets(data,200 * sizeof(char),stdin);
return JS_NewString(ctx,data);
}
static void js_console_init(JSContext *ctx,const char *name) {
JSValue global = JS_GetGlobalObject(ctx);
JSValue console = JS_NewObject(ctx);
JS_SetPropertyStr(ctx,console,"log",JS_NewCFunction(ctx,js_console_log,"log",1));
JS_SetPropertyStr(ctx,console,"assert",JS_NewCFunction(ctx,js_console_assert, "assert", 1));
JS_SetPropertyStr(ctx,console,"print",JS_NewCFunction(ctx,js_console_print,"print",1));
JS_SetPropertyStr(ctx,console,"prompt",JS_NewCFunction(ctx,js_console_prompt,"prompt",1));
JS_SetPropertyStr(ctx,console,"getpass",JS_NewCFunction(ctx,js_console_getpass,"getpass",1));
JS_SetPropertyStr(ctx,global,name,console);
JS_FreeValue(ctx,global);
}