Пример #1
0
JSBool
Output_write (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    JSString* string;

    if (argc < 1 || !JS_ConvertArguments(cx, argc, argv, "S", &string)) {
        JS_ReportError(cx, "Not enough parameters.");
        JS_LeaveLocalRootScope(cx);
        JS_EndRequest(cx);
        return JS_FALSE;
    }

    jsval property;

    JS_GetProperty(cx, obj, "content", &property);
    property = STRING_TO_JSVAL(JS_ConcatStrings(cx, JS_ValueToString(cx, property), string));
    JS_SetProperty(cx, obj, "content", &property);

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #2
0
void
__Window_options (JSContext* cx, WINDOW* win, JSObject* options, JSBool apply)
{
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    jsval jsAttrs; JS_GetProperty(cx, options, "at", &jsAttrs);
    if (!JSVAL_IS_INT(jsAttrs)) {
        JS_GetProperty(cx, options, "attribute", &jsAttrs);

        if (!JSVAL_IS_INT(jsAttrs)) {
            JS_GetProperty(cx, options, "attributes", &jsAttrs);
        }
    }

    if (JSVAL_IS_INT(jsAttrs)) {
        if (apply) {
            wattron(win, JSVAL_TO_INT(jsAttrs));
        }
        else {
            wattroff(win, JSVAL_TO_INT(jsAttrs));
        }
    }

    jsval jsForeground; JS_GetProperty(cx, options, "fg", &jsForeground);
    if (!JSVAL_IS_INT(jsForeground)) {
        JS_GetProperty(cx, options, "foreground", &jsForeground);
    }

    jsval jsBackground; JS_GetProperty(cx, options, "bg", &jsBackground);
    if (!JSVAL_IS_INT(jsBackground)) {
        JS_GetProperty(cx, options, "background", &jsBackground);
    }

    short fg = JSVAL_IS_INT(jsForeground) ? JSVAL_TO_INT(jsForeground) : -1;
    short bg = JSVAL_IS_INT(jsBackground) ? JSVAL_TO_INT(jsBackground) : -1;

    if (fg == -1 && bg == -1) {
        JS_LeaveLocalRootScope(cx);
        JS_EndRequest(cx);
        return;
    }

    char pair[3] = {0};
    sprintf(&pair[0], "%1d", (fg<0?0:fg)); // I've to give 0 if it's using the default
    sprintf(&pair[1], "%1d", (bg<0?0:bg)); // value because it f***s up with -1

    short c_pair = atoi(pair);
    if (apply) {
        init_pair(c_pair, fg, bg);
        wattron(win, COLOR_PAIR(c_pair));
    }
    else {
        wattroff(win, COLOR_PAIR(c_pair));
    }

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);
}
Пример #3
0
static JSBool
gjs_print_parse_args(JSContext *context,
                     uintN      argc,
                     jsval     *argv,
                     char     **buffer)
{
    GString *str;
    gchar *s;
    guint n;

    JS_BeginRequest(context);

    str = g_string_new("");
    (void)JS_EnterLocalRootScope(context);
    for (n = 0; n < argc; ++n) {
        JSExceptionState *exc_state;
        JSString *jstr;

        /* JS_ValueToString might throw, in which we will only
         * log that the value could be converted to string */
        exc_state = JS_SaveExceptionState(context);

        jstr = JS_ValueToString(context, argv[n]);
        if (jstr != NULL)
            argv[n] = STRING_TO_JSVAL(jstr); // GC root

        JS_RestoreExceptionState(context, exc_state);

        if (jstr != NULL) {
            if (!gjs_string_to_utf8(context, STRING_TO_JSVAL(jstr), &s)) {
                JS_LeaveLocalRootScope(context);
                JS_EndRequest(context);
                g_string_free(str, TRUE);
                return JS_FALSE;
            }

            g_string_append(str, s);
            g_free(s);
            if (n < (argc-1))
                g_string_append_c(str, ' ');
        } else {
            JS_LeaveLocalRootScope(context);
            JS_EndRequest(context);
            *buffer = g_string_free(str, TRUE);
            if (!*buffer)
                *buffer = g_strdup("<invalid string>");
            return JS_TRUE;
        }

    }
    JS_LeaveLocalRootScope(context);
    *buffer = g_string_free(str, FALSE);

    JS_EndRequest(context);
    return JS_TRUE;
}
Пример #4
0
JSBool
Window_getChar (JSContext* cx, JSObject* object, uintN argc, jsval* argv, jsval* rval)
{
    JSObject* options;

    WINDOW* win = (WINDOW*) JS_GetPrivate(cx, object);

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    if (argc == 0) {
        *rval = INT_TO_JSVAL(wgetch(win));
    }
    else {
        JS_ValueToObject(cx, argv[0], &options);

        if (!options) {
            JS_ReportError(cx, "Options isn't a valid object.");

            JS_LeaveLocalRootScope(cx);
            JS_EndRequest(cx);
            return JS_FALSE;
        }

        jsval x, y;

        JS_GetProperty(cx, options, "x", &x);
        if (JSVAL_IS_VOID(x) || JSVAL_IS_NULL(x)) {
            JS_GetProperty(cx, options, "X", &x);
        }

        JS_GetProperty(cx, options, "y", &y);
        if (JSVAL_IS_VOID(y) || JSVAL_IS_NULL(y)) {
            JS_GetProperty(cx, options, "Y", &y);
        }

        if (!JSVAL_IS_INT(x) || !JSVAL_IS_INT(y)) {
            JS_ReportError(cx, "An option is missing or isn't an int.");

            JS_LeaveLocalRootScope(cx);
            JS_EndRequest(cx);
            return JS_FALSE;
        }

        *rval = INT_TO_JSVAL(mvwgetch(win, JSVAL_TO_INT(y), JSVAL_TO_INT(x)));
    }

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #5
0
JSBool
Output_initialize (JSContext* cx)
{
    JSBool result = JS_FALSE;

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    jsval property;

    JSObject* output = JS_DefineObject(cx, ((LCGIData*) JS_GetContextPrivate(cx))->global, 
        "Output", &Output_class, NULL, 0
    );

    if (output) {
        property = INT_TO_JSVAL(1024);
        JS_SetProperty(cx, output, "limit", &property);
        property = JSVAL_FALSE;
        JS_SetProperty(cx, output, "buffered", &property);
        property = JS_GetEmptyStringValue(cx);
        JS_SetProperty(cx, output, "content", &property);

        result = JS_TRUE;
    }
    
    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return result;
}
Пример #6
0
JSBool 
js_get_descriptor(JSContext *cx, JSObject *obj,
		    uintN argc, jsval *argv, jsval *rval)
{
  char pdesc[PATH_MAX];
  char *pe, *desc;
  char errbuf[128];
  JSObject *o;
  js_plugin_t *jsp = JS_GetPrivate(cx, obj);

  snprintf(pdesc, sizeof(pdesc),"%s", jsp->jsp_url);
  pe = strrchr(pdesc, '/');
  if (pe == NULL)
    return JS_FALSE;

  snprintf(pe + 1, sizeof(pdesc) - (pe - pdesc), "plugin.json");

  desc = fa_load(pdesc, NULL, NULL, errbuf, sizeof(errbuf), 
		NULL, 0, NULL, NULL);
  if (desc == NULL) {
    TRACE(TRACE_ERROR, "JS", "Unable to read %s -- %s", pdesc, errbuf);
    return JS_FALSE;
  }

  if (!JS_EnterLocalRootScope(cx))
    return JS_FALSE;

  o = json_deserialize(desc, &json_to_jsapi, cx, errbuf, sizeof(errbuf));
  free(desc);
  *rval = OBJECT_TO_JSVAL(o);

  JS_LeaveLocalRootScope(cx);

  return JS_TRUE;
}
Пример #7
0
JSString*
__Window_readLine (JSContext* cx, WINDOW* win, JSBool moveFirst, jsval x, jsval y)
{
    char* string  = (char*) JS_malloc(cx, 16*sizeof(char));
    size_t length = 0;

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    string[0] = (moveFirst
        ? mvwgetch(win, JSVAL_TO_INT(y), JSVAL_TO_INT(x))
        : wgetch(win));
    
    while (string[(++length)-1] != '\n') {
        if ((length+1) % 16) {
            string = (char*) JS_realloc(cx, string, (length+16+1)*sizeof(char));
        }
    
        string[length] = (char) wgetch(win);
    }

    string[length-1] = '\0';
    string = (char*) JS_realloc(cx, string, length*sizeof(char));

    JSString* jsString = JS_NewString(cx, string, strlen(string));

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);
    return jsString;
}
Пример #8
0
JSBool 
js_json_decode(JSContext *cx, JSObject *obj,
	       uintN argc, jsval *argv, jsval *rval)
{
  char *str;
  JSObject *o;
  char errbuf[256];
  if(!JS_ConvertArguments(cx, argc, argv, "s", &str))
    return JS_FALSE;

  if(!JS_EnterLocalRootScope(cx))
    return JS_FALSE;

  o = json_deserialize(str, &json_to_jsapi, cx, errbuf, sizeof(errbuf));

  *rval = OBJECT_TO_JSVAL(o);

  JS_LeaveLocalRootScope(cx);

  if(o == NULL) {
    JS_ReportError(cx, "Invalid JSON -- %s", errbuf);
    return JS_FALSE;
  }
  return JS_TRUE;
}
Пример #9
0
JSBool
Window_redraw (JSContext* cx, JSObject* object, uintN argc, jsval* argv, jsval* rval)
{
    if (argc != 2 && argc != 0) {
        JS_ReportError(cx, "Not enough parameters.");
        return JS_FALSE;
    }

    WINDOW* win = ((WindowInformation*)JS_GetPrivate(cx, object))->win;

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);
    switch (argc) {
        case 0: {
            wrefresh(win);
        } break;

        case 2: {
            jsint beg; JS_ValueToInt32(cx, argv[0], &beg);
            jsint num; JS_ValueToInt32(cx, argv[1], &num);

            wredrawln(win, beg, num);
        } break;
    }
    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;

}
Пример #10
0
JSBool
TCP_accept (JSContext* cx, JSObject* object, uintN argc, jsval* argv, jsval* rval)
{
    jsdouble timeout = -1;

    if (argc) {
        JS_ValueToNumber(cx, argv[0], &timeout);
    }

    TCPInformation* data = (TCPInformation*) JS_GetPrivate(cx, object);
    
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    JSObject* sock = JS_NewObject(cx, &TCP_class, JS_GetPrototype(cx, object), NULL);
    TCPInformation* newData = new TCPInformation;
    JS_SetPrivate(cx, sock, newData);

    PRNetAddr addr;
    newData->socket = PR_Accept(data->socket, &addr, (timeout == -1)
        ? PR_INTERVAL_NO_TIMEOUT
        : PR_MicrosecondsToInterval(timeout*1000000));

    jsval property = JSVAL_TRUE;
    JS_SetProperty(cx, sock, "connected", &property);

    *rval = OBJECT_TO_JSVAL(sock);

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);
    return JS_TRUE;
}
Пример #11
0
void
gjs_explain_scope(JSContext  *context,
                  const char *title)
{
    JSContext *load_context;
    JSContext *call_context;
    JSObject *global;
    JSObject *parent;
    GString *chain;

    gjs_debug(GJS_DEBUG_SCOPE,
              "=== %s ===",
              title);

    load_context = gjs_runtime_peek_load_context(JS_GetRuntime(context));
    call_context = gjs_runtime_peek_call_context(JS_GetRuntime(context));

    JS_BeginRequest(context);
    JS_BeginRequest(load_context);
    JS_BeginRequest(call_context);

    JS_EnterLocalRootScope(context);

    gjs_debug(GJS_DEBUG_SCOPE,
              "  Context: %p %s",
              context,
              context == load_context ? "(LOAD CONTEXT)" :
              context == call_context ? "(CALL CONTEXT)" :
              "");

    global = JS_GetGlobalObject(context);
    gjs_debug(GJS_DEBUG_SCOPE,
              "  Global: %p %s",
              global, gjs_value_debug_string(context, OBJECT_TO_JSVAL(global)));

    parent = JS_GetScopeChain(context);
    chain = g_string_new(NULL);
    while (parent != NULL) {
        const char *debug;
        debug = gjs_value_debug_string(context, OBJECT_TO_JSVAL(parent));

        if (chain->len > 0)
            g_string_append(chain, ", ");

        g_string_append_printf(chain, "%p %s",
                               parent, debug);
        parent = JS_GetParent(context, parent);
    }
    gjs_debug(GJS_DEBUG_SCOPE,
              "  Chain: %s",
              chain->str);
    g_string_free(chain, TRUE);

    JS_LeaveLocalRootScope(context);

    JS_EndRequest(call_context);
    JS_EndRequest(load_context);
    JS_EndRequest(context);
}
Пример #12
0
JSBool
Window_printChar (JSContext* cx, JSObject* object, uintN argc, jsval* argv, jsval* rval)
{
    if (argc < 1) {
        JS_ReportError(cx, "Not enough parameters.");
        return JS_FALSE;
    }

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    WINDOW* win = (WINDOW*) JS_GetPrivate(cx, object);

    jsint ch; JS_ValueToInt32(cx, argv[0], &ch);

    if (argc == 1){
        waddch(win, ch);
    }
    else if (argc == 2) {
        JSObject* options; JS_ValueToObject(cx, argv[1], &options);

        jsval x, y;

        JS_GetProperty(cx, options, "x", &x);
        if (JSVAL_IS_VOID(x) || JSVAL_IS_NULL(x)) {
            JS_GetProperty(cx, options, "X", &x);
        }

        JS_GetProperty(cx, options, "y", &y);
        if (JSVAL_IS_VOID(y) || JSVAL_IS_NULL(y)) {
            JS_GetProperty(cx, options, "Y", &y);
        }

        jsval jsEcho; JS_GetProperty(cx, options, "echo", &jsEcho);
        JSBool echo; JS_ValueToBoolean(cx, jsEcho, &echo);

        __Window_options(cx, win, options, JS_TRUE);
        if (echo) {
            wechochar(win, ch);
        }
        else if (!JSVAL_IS_INT(x) && !JSVAL_IS_INT(y)) {
            waddch(win, ch);
        }
        else {
            mvwaddch(win,
                JSVAL_IS_INT(y) ? JSVAL_TO_INT(y) : 0,
                JSVAL_IS_INT(x) ? JSVAL_TO_INT(x) : 0,
                ch
            );
        }
        __Window_options(cx, win, options, JS_FALSE);
    }

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #13
0
void
gjs_log_object_props(JSContext      *context,
                     JSObject       *obj,
                     GjsDebugTopic   topic,
                     const char     *prefix)
{
    JSObject *props_iter;
    jsid prop_id;

    JS_BeginRequest(context);

    /* We potentially create new strings, plus the property iterator,
     * that could get collected as we go through this process. So
     * create a local root scope.
     */
    JS_EnterLocalRootScope(context);

    props_iter = JS_NewPropertyIterator(context, obj);
    if (props_iter == NULL) {
        gjs_debug(GJS_DEBUG_ERROR,
                  "Failed to create property iterator for object props");
        goto done;
    }

    prop_id = JSVAL_VOID;
    if (!JS_NextProperty(context, props_iter, &prop_id))
        goto done;

    while (prop_id != JSVAL_VOID) {
        jsval nameval;
        const char *name;
        jsval propval;

        if (!JS_IdToValue(context, prop_id, &nameval))
            goto next;

        if (!gjs_get_string_id(nameval, &name))
            goto next;

        if (!gjs_object_get_property(context, obj, name, &propval))
            goto next;

        gjs_debug(topic,
                  "%s%s = '%s'",
                  prefix, name,
                  gjs_value_debug_string(context, propval));

    next:
        prop_id = JSVAL_VOID;
        if (!JS_NextProperty(context, props_iter, &prop_id))
            break;
    }

 done:
    JS_LeaveLocalRootScope(context);
    JS_EndRequest(context);
}
Пример #14
0
void
js_prop_set_from_jsval(JSContext *cx, prop_t *p, jsval value)
{
  JSBool b;
  if(JSVAL_IS_INT(value)) {
    prop_set_int(p, JSVAL_TO_INT(value));
  } else if(JSVAL_IS_BOOLEAN(value)) {
    prop_set_int(p, JSVAL_TO_BOOLEAN(value));
  } else if(JSVAL_IS_NULL(value) || JSVAL_IS_VOID(value)) {
    prop_set_void(p);
  } else if(JSVAL_IS_DOUBLE(value)) {
    double d;
    if(JS_ValueToNumber(cx, value, &d))
      prop_set_float(p, d);
  } else if(JS_HasInstance(cx, RichText, value, &b) && b) {
    JSObject *o = JSVAL_TO_OBJECT(value);
    jsval v2;

    if(!JS_EnterLocalRootScope(cx))
      return;

    if(!JS_GetProperty(cx, o, "text", &v2)) {
      JS_LeaveLocalRootScope(cx);
      return;
    }

    prop_set_string_ex(p, NULL, JS_GetStringBytes(JS_ValueToString(cx, v2)),
		       PROP_STR_RICH);
    JS_LeaveLocalRootScope(cx);
  } else if(JSVAL_IS_STRING(value)) {
    js_prop_from_str(cx, p, value);
  } else if(JSVAL_IS_OBJECT(value)) {
    JSObject *obj = JSVAL_TO_OBJECT(value);
    JSClass *c = JS_GetClass(cx, obj);

    if(!strcmp(c->name, "XML"))   // Treat some classes special
      js_prop_from_str(cx, p, value);
    else
      js_prop_from_object(cx, obj, p);
  } else {
    prop_set_void(p);
  }
}
Пример #15
0
JSBool
Core_print (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    char*     separator = " ";
    char*     end       = "\n";
    int       fd        = fileno(stdout);
    FILE*     fp        = NULL;
    jsval     property;
    JSObject* options;

    if (argc > 1 && JS_TypeOfValue(cx, argv[argc-1]) == JSTYPE_OBJECT) {
        JS_ValueToObject(cx, argv[argc-1], &options);
        argc--;

        JS_GetProperty(cx, options, "separator", &property);
        if (JSVAL_IS_VOID(property) || JSVAL_IS_NULL(property)) {
            JS_GetProperty(cx, options, "sep", &property);
        }

        if (JSVAL_IS_STRING(property)) {
            separator = JS_GetStringBytes(JS_ValueToString(cx, property));
        }

        JS_GetProperty(cx, options, "end", &property);
        if (JSVAL_IS_STRING(property)) {
            end = JS_GetStringBytes(JS_ValueToString(cx, property));
        }

        JS_GetProperty(cx, options, "file", &property);
        if (JSVAL_IS_NUMBER(property)) {
            fd = JSVAL_TO_INT(property);
        }
    }

    fp = fdopen(fd, "a+");

    uintN i;
    for (i = 0; i < argc; i++) {
        fprintf(fp, "%s", JS_GetStringBytes(JS_ValueToString(cx, argv[i])));

        if (i != argc-1) {
            fprintf(fp, "%s", separator);
        }
    }
    fprintf(fp, "%s", end);

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #16
0
JSBool
Window_printString (JSContext* cx, JSObject* object, uintN argc, jsval* argv, jsval* rval)
{
    if (argc < 1) {
        JS_ReportError(cx, "Not enough parameters.");
        return JS_FALSE;
    }

    WINDOW* win = (WINDOW*) JS_GetPrivate(cx, object);

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    if (argc == 1){
        wprintw(win, JS_GetStringBytes(JS_ValueToString(cx, argv[0])));
    }
    else if (argc == 2) {
        JSObject* options; JS_ValueToObject(cx, argv[1], &options);

        jsval x, y;

        JS_GetProperty(cx, options, "x", &x);
        if (JSVAL_IS_VOID(x) || JSVAL_IS_NULL(x)) {
            JS_GetProperty(cx, options, "X", &x);
        }

        JS_GetProperty(cx, options, "y", &y);
        if (JSVAL_IS_VOID(y) || JSVAL_IS_NULL(y)) {
            JS_GetProperty(cx, options, "Y", &y);
        }

        __Window_options(cx, win, options, JS_TRUE);
        if (!JSVAL_IS_INT(x) && !JSVAL_IS_INT(y)) {
            wprintw(win, JS_GetStringBytes(JS_ValueToString(cx, argv[0])));
        }
        else {
            mvwprintw(win,
                JSVAL_IS_INT(y) ? JSVAL_TO_INT(y) : 0,
                JSVAL_IS_INT(x) ? JSVAL_TO_INT(x) : 0,
                JS_GetStringBytes(JS_ValueToString(cx, argv[0]))
            );
        }
        __Window_options(cx, win, options, JS_FALSE);
    }

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #17
0
void
js_prop_set_from_jsval(JSContext *cx, prop_t *p, jsval value, int recurse)
{
  JSBool b;
  if(JSVAL_IS_INT(value)) {
    prop_set_int(p, JSVAL_TO_INT(value));
  } else if(JSVAL_IS_BOOLEAN(value)) {
    prop_set_int(p, JSVAL_TO_BOOLEAN(value));
  } else if(JSVAL_IS_DOUBLE(value)) {
    double d;
    if(JS_ValueToNumber(cx, value, &d))
      prop_set_float(p, d);
  } else if(JS_HasInstance(cx, RichText, value, &b) && b) {
    JSObject *o = JSVAL_TO_OBJECT(value);
    jsval v2;

    if(!JS_EnterLocalRootScope(cx))
      return;

    if(!JS_GetProperty(cx, o, "text", &v2)) {
      JS_LeaveLocalRootScope(cx);
      return;
    }

    prop_set_string_ex(p, NULL, JS_GetStringBytes(JS_ValueToString(cx, v2)),
		       PROP_STR_RICH);
    JS_LeaveLocalRootScope(cx);

  } else if(JSVAL_IS_VOID(value) || JSVAL_IS_NULL(value)) {
    prop_set_void(p);
  } else if(recurse && JSVAL_IS_OBJECT(value)) {
    js_prop_from_object(cx, JSVAL_TO_OBJECT(value), p, recurse);
  } else {
    prop_set_string(p, JS_GetStringBytes(JS_ValueToString(cx, value)));
  }
}
Пример #18
0
static JSBool
js_db_step(JSContext *cx, JSObject *obj, uintN argc,
           jsval *argv, jsval *rval)
{
    js_db_t *jd = JS_GetPrivate(cx, obj);

    if(js_db_check(cx, jd))
        return JS_FALSE;

    if(jd->jd_stmt == NULL) {
        *rval = JSVAL_NULL;
    } else if(jd->jd_step_rc == SQLITE_ROW) {
        int cols = sqlite3_data_count(jd->jd_stmt);
        int i;

        JSObject *r = JS_NewObjectWithGivenProto(cx, NULL, NULL, obj);
        *rval = OBJECT_TO_JSVAL(r);

        if(!JS_EnterLocalRootScope(cx))
            return JS_FALSE;

        for(i = 0; i < cols; i++) {

            const char *cn = sqlite3_column_name(jd->jd_stmt, i);

            switch(sqlite3_column_type(jd->jd_stmt, i)) {
            case SQLITE_INTEGER:
                js_set_prop_int(cx, r, cn, sqlite3_column_int(jd->jd_stmt, i));
                break;
            case SQLITE_TEXT:
                js_set_prop_str(cx, r, cn,
                                (const char *)sqlite3_column_text(jd->jd_stmt, i));
                break;
            case SQLITE_FLOAT:
                js_set_prop_dbl(cx, r, cn, sqlite3_column_double(jd->jd_stmt, i));
                break;
            }
        }
        JS_LeaveLocalRootScope(cx);

        return js_stmt_step(cx, jd, rval);
    }
    *rval = JSVAL_FALSE;
    return JS_TRUE;
}
Пример #19
0
JSBool
Core_exec (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    FILE* pipe;
    char* output  = NULL;
    size_t length = 0;
    size_t read   = 0;
    const char* command;

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    if (argc != 1 || !JS_ConvertArguments(cx, argc, argv, "s", &command)) {
        JS_ReportError(cx, "Not enough parameters.");
        return JS_FALSE;
    }
    
    jsrefcount req = JS_SuspendRequest(cx);
    if ((pipe = popen(command, "r")) == NULL) {
        JS_ReportError(cx, "Command not found");
        return JS_FALSE;
    }

    // Read untill the pipe is empty.
    while (1) {
        output = (char*) JS_realloc(cx, output, length+=512);
        read   = fread(output+(length-512), sizeof(char), 512, pipe);

        if (read < 512) {
            output = (char*) JS_realloc(cx, output, length-=(512-read));
            break;
        }
    }
    output[length-1] = '\0';
    pclose(pipe);
    JS_ResumeRequest(cx, req);

    *rval = STRING_TO_JSVAL(JS_NewString(cx, output, length));

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #20
0
JSObject*
gjs_define_string_array(JSContext   *context,
                        JSObject    *in_object,
                        const char  *array_name,
                        gssize       array_length,
                        const char **array_values,
                        uintN        attrs)
{
    GArray *elems;
    JSObject *array;
    int i;

    JS_BeginRequest(context);

    if (!JS_EnterLocalRootScope(context))
        return JS_FALSE;

    if (array_length == -1)
        array_length = g_strv_length((char**)array_values);

    elems = g_array_sized_new(FALSE, FALSE, sizeof(jsval), array_length);

    for (i = 0; i < array_length; ++i) {
        jsval element;
        element = STRING_TO_JSVAL(JS_NewStringCopyZ(context, array_values[i]));
        g_array_append_val(elems, element);
    }

    array = JS_NewArrayObject(context, elems->len, (jsval*) elems->data);
    g_array_free(elems, TRUE);

    if (array != NULL) {
        if (!JS_DefineProperty(context, in_object,
                               array_name, OBJECT_TO_JSVAL(array),
                               NULL, NULL, attrs))
            array = NULL;
    }

    JS_LeaveLocalRootScope(context);

    JS_EndRequest(context);
    return array;
}
Пример #21
0
JSBool
Output_flush (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    jsval property;
    JS_GetProperty(cx, obj, "content", &property);

    FCGX_PutS(JS_GetStringBytes(JS_ValueToString(cx, property)), ((LCGIData*) JS_GetContextPrivate(cx))->cgi->out);

    property = JS_GetEmptyStringValue(cx);
    JS_SetProperty(cx, obj, "content", &property);

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #22
0
JSObject*
Core_initialize (JSContext *cx, const char* path)
{
    JS_BeginRequest(cx);
    JS_SetOptions(cx, JSOPTION_VAROBJFIX|JSOPTION_JIT|JSOPTION_XML);
    JS_SetVersion(cx, JS_StringToVersion("1.8"));

    JSObject* object = JS_NewObject(cx, &Core_class, NULL, NULL);

    if (object && JS_InitStandardClasses(cx, object)) {
        JS_DefineFunctions(cx, object, Core_methods);

        JS_EnterLocalRootScope(cx);

        // Properties
        jsval property;

        std::string rootPath = __Core_getRootPath(cx, path);
        jsval paths[] = {
            STRING_TO_JSVAL(JS_NewString(cx, JS_strdup(cx, rootPath.c_str()), rootPath.length())),
            STRING_TO_JSVAL(JS_NewString(cx, JS_strdup(cx, __LJS_LIBRARY_PATH__), strlen(__LJS_LIBRARY_PATH__)))
        };
        property = OBJECT_TO_JSVAL(JS_NewArrayObject(cx, 2, paths));
        JS_SetProperty(cx, object, "__PATH__", &property);

        property = STRING_TO_JSVAL(JS_NewString(cx, JS_strdup(cx, __LJS_VERSION__), strlen(__LJS_VERSION__)));
        JS_SetProperty(cx, object, "__VERSION__", &property);

        property = OBJECT_TO_JSVAL(object);
        JS_SetProperty(cx, object, "Program", &property);

        JS_LeaveLocalRootScope(cx);
        JS_EndRequest(cx);

        if (__Core_include(cx, __LJS_LIBRARY_PATH__ "/Core")) {
            return object;
        }
    }

    return NULL;
}
Пример #23
0
JSBool
js_cache_get(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  void *value;
  size_t vsize;
  char stash[256];
  char errbuf[256];
  const char *key,*lstash;
  JSObject *o;

  js_plugin_t *jsp = JS_GetPrivate(cx, obj);

  if (!JS_ConvertArguments(cx, argc, argv, "ss", &lstash, &key))
    return JS_FALSE;

  // fetch json from cache
  snprintf(stash, sizeof(stash), "plugin/%s/%s", jsp->jsp_id, lstash);
  value = blobcache_get(key, stash, &vsize, 0, NULL, NULL, NULL);

  if(value == NULL) {
    *rval = OBJECT_TO_JSVAL(NULL);
    return JS_TRUE;
  }

  // deserialize into json object
  if(!JS_EnterLocalRootScope(cx))
    return JS_FALSE;

  o = json_deserialize(value, &json_to_jsapi, cx, errbuf, sizeof(errbuf));

  *rval = OBJECT_TO_JSVAL(o);

  JS_LeaveLocalRootScope(cx);

  if(o == NULL) {
    JS_ReportError(cx, "Invalid JSON stored in cache -- %s", errbuf);
    return JS_FALSE;
  }

  return JS_TRUE;
}
Пример #24
0
JSBool
TCP_initialize (JSContext* cx)
{
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    JSObject* parent = JSVAL_TO_OBJECT(JS_EVAL(cx, "System.Network.Sockets"));

    JSObject* object = JS_InitClass(
        cx, parent, JSVAL_TO_OBJECT(JS_EVAL(cx, "System.Network.Sockets.prototype")), &TCP_class,
        TCP_constructor, 1, TCP_attributes, TCP_methods, NULL, TCP_static_methods
    );

    if (object) {
        JS_LeaveLocalRootScope(cx);
        JS_EndRequest(cx);
        return JS_TRUE;
    }

    return JS_FALSE;
}
Пример #25
0
void
__Window_updatePosition (JSContext* cx, JSObject* object)
{
    int y, x;
    WINDOW* win = (WINDOW*) JS_GetPrivate(cx, object);
    getbegyx(win, y, x);

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    jsval jsPosition; JS_GetProperty(cx, object, "Position", &jsPosition);
    JSObject* Position = JSVAL_TO_OBJECT(jsPosition);

    jsval property;
    property = INT_TO_JSVAL(y);
    JS_SetProperty(cx, Position, "Y", &property);
    property = INT_TO_JSVAL(x);
    JS_SetProperty(cx, Position, "X", &property);

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);
}
Пример #26
0
JSBool
Output_content_set (JSContext *cx, JSObject *obj, jsval idval, jsval *vp)
{
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    jsval     property;
    int       limit;
    JSBool    buffered;
    JSString* string;

    JS_GetProperty(cx, obj, "limit", &property);
    JS_ValueToInt32(cx, property, &limit);
    JS_GetProperty(cx, obj, "buffered", &property);
    JS_ValueToBoolean(cx, property, &buffered);
    string = JS_ValueToString(cx, *vp);

    if (!buffered && JS_GetStringLength(string) > limit) {
        if (!JSVAL_TO_BOOLEAN(JS_EVAL(cx, "Headers.sent"))) {
            JS_EVAL(cx, "Headers.send()");
        }

        FCGX_Stream* out       = ((LCGIData*) JS_GetContextPrivate(cx))->cgi->out;
        char*        cString   = JS_GetStringBytes(string);
        char         size[300] = {NULL};
        sprintf(size, "%x", strlen(cString));

        FCGX_FPrintF(out, "%s\r\n%s\r\n", size, cString);

        property = JS_GetEmptyStringValue(cx);
        JS_SetProperty(cx, obj, "content", &property);
    }

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);

    return JS_TRUE;
}
Пример #27
0
JSBool
Window_initialize (JSContext* cx)
{
    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    jsval jsParent;
    JS_GetProperty(cx, JS_GetGlobalObject(cx), "ncurses", &jsParent);
    JSObject* parent = JSVAL_TO_OBJECT(jsParent);

    JSObject* object = JS_InitClass(
        cx, parent, NULL, &Window_class,
        Window_constructor, 1, NULL, Window_methods, NULL, NULL
    );

    if (object) {
        JS_LeaveLocalRootScope(cx);
        JS_EndRequest(cx);
        return JS_TRUE;
    }

    return JS_FALSE;
}
Пример #28
0
jsval
gjs_date_from_time_t (JSContext *context, time_t time)
{
    JSObject *date;
    JSClass *date_class;
    JSObject *date_constructor;
    jsval date_prototype;
    jsval args[1];
    jsval result;

    JS_BeginRequest(context);

    if (!JS_EnterLocalRootScope(context))
        return JSVAL_VOID;

    if (!JS_GetClassObject(context, JS_GetGlobalObject(context), JSProto_Date,
                           &date_constructor))
        gjs_fatal("Failed to lookup Date prototype");

    if (!JS_GetProperty(context, date_constructor, "prototype", &date_prototype))
        gjs_fatal("Failed to get prototype from Date constructor");

    date_class = JS_GET_CLASS(context, JSVAL_TO_OBJECT (date_prototype));

    if (!JS_NewNumberValue(context, ((double) time) * 1000, &(args[0])))
        gjs_fatal("Failed to convert time_t to number");

    date = JS_ConstructObjectWithArguments(context, date_class,
                                           NULL, NULL, 1, args);

    result = OBJECT_TO_JSVAL(date);
    JS_LeaveLocalRootScope(context);
    JS_EndRequest(context);

    return result;
}
Пример #29
0
void
__Window_updateSize (JSContext* cx, JSObject* object)
{
    int height, width;
    WINDOW* win = (WINDOW*) JS_GetPrivate(cx, object);
    getmaxyx(win, height, width);

    JS_BeginRequest(cx);
    JS_EnterLocalRootScope(cx);

    jsval property;
    JS_GetProperty(cx, object, "Size", &property);
    JSObject* Size = JSVAL_TO_OBJECT(property);
    JS_GetProperty(cx, object, "border", &property);
    JSBool border = JSVAL_TO_BOOLEAN(property);

    property = INT_TO_JSVAL(border ? height-2 : height);
    JS_SetProperty(cx, Size, "Height", &property);
    property = INT_TO_JSVAL(border ? width-2 : width);
    JS_SetProperty(cx, Size, "Width", &property);

    JS_LeaveLocalRootScope(cx);
    JS_EndRequest(cx);
}
Пример #30
0
/* [implicit_jscontext] dpoIData allocateData2 (in dpoIData templ, [optional] in uint32_t length); */
NS_IMETHODIMP dpoCContext::AllocateData2(dpoIData *templ, uint32_t length, JSContext *cx, dpoIData **_retval) 
{
	// this cast is only safe as long as no other implementations of the dpoIData interface exist
	dpoCData *cData = (dpoCData *) templ;
	cl_int err_code;
	nsresult result;
	size_t bytePerElements;
	nsCOMPtr<dpoCData> data;
#ifdef PREALLOCATE_IN_JS_HEAP
	jsval jsBuffer;
#endif /* PREALLOCATE_IN_JS_HEAP */

	if (!JS_EnterLocalRootScope(cx)) {
		DEBUG_LOG_STATUS("AllocateData2", "Cannot root local scope");
		return NS_ERROR_NOT_AVAILABLE;
	}

	data = new dpoCData( this);
	if (data == NULL) {
		DEBUG_LOG_STATUS("AllocateData2", "Cannot create new dpoCData object");
		return NS_ERROR_OUT_OF_MEMORY;
	}
	
	if (length == 0) {
		DEBUG_LOG_STATUS("AllocateData2", "length not provided, assuming template's size");
		length = cData->GetLength();
	}

	bytePerElements = cData->GetSize() / cData->GetLength();

	DEBUG_LOG_STATUS("AllocateData2", "length " << length << " bytePerElements " << bytePerElements);

#ifdef PREALLOCATE_IN_JS_HEAP
	JSObject *jsArray;
	if (NS_FAILED(CreateAlignedTA(cData->GetType(), length, &jsArray, cx))) {
		return NS_ERROR_NOT_AVAILABLE;
	}
	if (!jsArray) {
		DEBUG_LOG_STATUS("AllocateData2", "Cannot create typed array");
		return NS_ERROR_OUT_OF_MEMORY;
	}

	cl_mem memObj = CreateBuffer(CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, 
                                 JS_GetTypedArrayByteLength(jsArray, cx), JS_GetArrayBufferViewData(jsArray, cx), &err_code);
#else /* PREALLOCATE_IN_JS_HEAP */
	JSObject *jsArray = NULL;
	cl_mem memObj = CreateBuffer(CL_MEM_READ_WRITE, length * bytePerElements, NULL, &err_code);
#endif /* PREALLOCATE_IN_JS_HEAP */
	if (err_code != CL_SUCCESS) {
		DEBUG_LOG_ERROR("AllocateData2", err_code);
		return NS_ERROR_NOT_AVAILABLE;
	}

	result = data->InitCData(cx, cmdQueue, memObj, cData->GetType(), length, length * bytePerElements, jsArray);

	if (NS_SUCCEEDED(result)) {
		data.forget((dpoCData **) _retval);
	}

	JS_LeaveLocalRootScope(cx);
		
    return result;
}