zibbr

Random 3d and p2p stuff that was originally the beginnings of an overly ambitious p2p virtual world thingy
git clone https://code.literati.org/zibbr.git
Log | Files | Refs

js-funcs.c (1047B)


      1 #include <stdio.h>
      2 #include <readline/readline.h>
      3 #include <jsapi.h>
      4 #include "js-funcs.h"
      5 
      6 static JSBool myjs_readline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
      7     const char *prompt;
      8 
      9     if (!JS_ConvertArguments(cx, argc, argv, "s", &prompt)) {
     10         return JS_FALSE;
     11     }
     12     char *line = readline(prompt);
     13     *rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, line));
     14     if (line && *line) {
     15         add_history(line);
     16     }
     17     free(line);
     18     return JS_TRUE;
     19 }
     20 
     21 static JSBool myjs_print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
     22     int i;
     23 
     24     for (i = 0; i < argc; i++) {
     25         fputs(JS_GetStringBytes(JS_ValueToString(cx, argv[i])), stdout);
     26     }
     27     return JS_TRUE;
     28 }
     29 
     30 static JSFunctionSpec myjs_global_functions[] = {
     31     {"readline", myjs_readline, 1, 0, 0},
     32     {"print", myjs_print, 1, 0, 0},
     33     NULL,
     34 };
     35 
     36 int setup_js_functions(JSContext *cx, JSObject *global) {
     37     if (!JS_DefineFunctions(cx, global, myjs_global_functions)) {
     38         return 0;
     39     }
     40     return 1;
     41 }
     42