Exemplo n.º 1
0
static JSBool
import_file_on_module(JSContext  *context,
                      JSObject   *obj,
                      const char *name,
                      GFile      *file)
{
    JSObject *module_obj;
    JSBool retval = JS_FALSE;
    char *full_path = NULL;

    module_obj = create_module_object (context);

    if (!define_import(context, obj, module_obj, name))
        goto out;

    if (!import_file(context, name, file, module_obj))
        goto out;

    full_path = g_file_get_parse_name (file);
    if (!define_meta_properties(context, module_obj, full_path, name, obj))
        goto out;

    if (!seal_import(context, obj, name))
        goto out;

    retval = JS_TRUE;

 out:
    if (!retval)
        cancel_import(context, obj, name);

    g_free (full_path);
    return retval;
}
Exemplo n.º 2
0
static JSBool
import_file_on_module(JSContext  *context,
                      JSObject   *obj,
                      std::string &name,
                      std::string &filePath)
{
    JSObject *module_obj;
    JSBool retval = JS_FALSE;

    module_obj = create_module_object (context);

    if (!define_import(context, obj, module_obj, name))
        goto out;

    if (!import_file(context, name, filePath, module_obj))
        goto out;

    if (!define_meta_properties(context, module_obj, filePath.c_str(), name, obj))
        goto out;

    if (!seal_import(context, obj, name))
        goto out;
    retval = JS_TRUE;

 out:
    if (!retval)
        cancel_import(context, obj, name);

    return retval;
}
Exemplo n.º 3
0
static JSBool
import_native_file(JSContext  *context,
                   JSObject   *obj,
                   const char *name,
                   const char *full_path)
{
    JSObject *module_obj;
    GjsNativeFlags flags;
    JSBool retval = JS_FALSE;

    gjs_debug(GJS_DEBUG_IMPORTER,
              "Importing '%s' from '%s'", name, full_path ? full_path : "<internal>");

    module_obj = JS_ConstructObject(context, NULL, NULL, NULL);
    if (module_obj == NULL) {
        return JS_FALSE;
    }

    /* We store the module object into the parent module before
     * initializing the module. If the module has the
     * GJS_NATIVE_SUPPLIES_MODULE_OBJ flag, it will just overwrite
     * the reference we stored when it initializes.
     */
    if (!define_import(context, obj, module_obj, name))
        return JS_FALSE;

    if (!define_meta_properties(context, module_obj, name, obj))
        goto out;

    if (!gjs_import_native_module(context, module_obj, full_path, &flags))
        goto out;

    if (!finish_import(context, name))
        goto out;

    if (!seal_import(context, obj, name))
        goto out;

    retval = JS_TRUE;

out:
    if (!retval)
        cancel_import(context, obj, name);

    return retval;
}
Exemplo n.º 4
0
static JSBool
import_file(JSContext  *context,
            JSObject   *obj,
            const char *name,
            const char *full_path)
{
    char *script;
    gsize script_len;
    JSObject *module_obj;
    GError *error;
    jsval script_retval;
    JSBool retval = JS_FALSE;

    gjs_debug(GJS_DEBUG_IMPORTER,
              "Importing '%s'", full_path);

    module_obj = JS_NewObject(context, NULL, NULL, NULL);
    if (module_obj == NULL) {
        return JS_FALSE;
    }

    if (!define_import(context, obj, module_obj, name))
        return JS_FALSE;

    if (!define_meta_properties(context, module_obj, full_path, name, obj))
        goto out;

    script_len = 0;
    error = NULL;

    if (!(g_file_get_contents(full_path, &script, &script_len, &error))) {
        if (!g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_ISDIR) &&
            !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOTDIR) &&
            !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
            gjs_throw_g_error(context, error);
        else
            g_error_free(error);

        goto out;
    }

    g_assert(script != NULL);

    if (!JS_EvaluateScript(context,
                           module_obj,
                           script,
                           script_len,
                           full_path,
                           1, /* line number */
                           &script_retval)) {
        g_free(script);

        /* If JSOPTION_DONT_REPORT_UNCAUGHT is set then the exception
         * would be left set after the evaluate and not go to the error
         * reporter function.
         */
        if (JS_IsExceptionPending(context)) {
            gjs_debug(GJS_DEBUG_IMPORTER,
                      "Module '%s' left an exception set",
                      name);
            gjs_log_and_keep_exception(context);
        } else {
            gjs_throw(context,
                         "JS_EvaluateScript() returned FALSE but did not set exception");
        }

        goto out;
    }

    g_free(script);

    if (!finish_import(context, name))
        goto out;

    if (!seal_import(context, obj, name))
        goto out;

    retval = JS_TRUE;

 out:
    if (!retval)
        cancel_import(context, obj, name);

    return retval;
}