
JSModuleDef *qjs_module_loader(JSContext *ctx, const char *module_name, void *opaque)
{
	JSModuleDef *m;
	const char *fext = gf_file_ext_start(module_name);

	if (fext && (!strcmp(fext, ".so") || !strcmp(fext, ".dll") || !strcmp(fext, ".dylib")) )  {
#ifndef GPAC_STATIC_BUILD
		m = qjs_module_loader_dyn_lib(ctx, module_name);
#else
		JS_ThrowReferenceError(ctx, "could not load module filename '%s', dynamic library loading disabled in build", module_name);
		m = NULL;
#endif
	} else {
		u32 buf_len;
		u8 *buf;
		JSValue func_val;
		char *url;
		const char *par_url = jsf_get_script_filename(ctx);
		url = gf_url_concatenate(par_url, module_name);

		GF_Err e = gf_file_load_data(url ? url : module_name, &buf, &buf_len);
		if (url) gf_free(url);

		if (e != GF_OK) {
			JS_ThrowReferenceError(ctx, "could not load module filename '%s': %s", module_name, gf_error_to_string(e) );
			return NULL;
		}
		/* compile the module */
		func_val = JS_Eval(ctx, buf ? (char *) buf : "", buf_len, module_name, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
		gf_free(buf);
		if (JS_IsException(func_val))
			return NULL;
		/* XXX: could propagate the exception */
		js_module_set_import_meta(ctx, func_val, GF_TRUE, GF_FALSE);
		/* the module is already referenced, so we must free it */
		m = JS_VALUE_GET_PTR(func_val);
		JS_FreeValue(ctx, func_val);
	}
	return m;

JSModuleDef *qjs_module_loader(JSContext *ctx, const char *module_name, void *opaque)
{
	JSModuleDef *m;
	const char *fext = gf_file_ext_start(module_name);

	if (fext && (!strcmp(fext, ".so") || !strcmp(fext, ".dll") || !strcmp(fext, ".dylib")) )  {
#ifndef GPAC_STATIC_BUILD
		m = qjs_module_loader_dyn_lib(ctx, module_name);
#else
		JS_ThrowReferenceError(ctx, "could not load module filename '%s', dynamic library loading disabled in build", module_name);
		m = NULL;
#endif
	} else {
		u32 buf_len;
		u8 *buf;
		JSValue func_val;
		char *url;
		const char *par_url = jsf_get_script_filename(ctx);
		url = gf_url_concatenate(par_url, module_name);

		GF_Err e = gf_file_load_data(url ? url : module_name, &buf, &buf_len);
		if (url) gf_free(url);

		if (e != GF_OK) {
			JS_ThrowReferenceError(ctx, "could not load module filename '%s': %s", module_name, gf_error_to_string(e) );
			return NULL;
		}
		 
		func_val = JS_Eval(ctx, buf ? (char *) buf : "", buf_len, module_name, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
		gf_free(buf);
		if (JS_IsException(func_val))
			return NULL;
		 
		js_module_set_import_meta(ctx, func_val, GF_TRUE, GF_FALSE);
		 
		m = JS_VALUE_GET_PTR(func_val);
		JS_FreeValue(ctx, func_val);
	}
	return m;