Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes and additional functions #252

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

zwarrior1
Copy link

@zwarrior1 zwarrior1 commented Mar 13, 2024

Hi, I have been using QuickJS as a wrapper for a long time.
these are the fixed I have made.

  • I used raw string access to avoid additional memory allocation
  • also new string allocate function for acts like JS_NewArrayBuffer

@zwarrior1 zwarrior1 changed the title Small chanes Small changes Mar 13, 2024
@zwarrior1 zwarrior1 changed the title Small changes Small fixes and additional functions Mar 13, 2024
Comment on lines -469 to +512
JS_ThrowReferenceError(ctx, "shared library modules are not supported yet");
return NULL;
JSModuleDef *m;
HANDLE hd;
JSInitModuleFunc *init;
char *filename;

if (!strchr(module_name, '/')) {
/* must add a '/' so that the DLL is not searched in the
system library paths */
filename = js_malloc(ctx, strlen(module_name) + 2 + 1);
if (!filename)
return NULL;
strcpy(filename, "./");
strcpy(filename + 2, module_name);
} else {
filename = (char *)module_name;
}

/* C module */
hd = LoadLibrary(filename);
if (filename != module_name)
js_free(ctx, filename);
if (!hd) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'('%s') as shared library",
module_name,filename);
goto fail;
}

init = (JSInitModuleFunc*)GetProcAddress(hd,"js_init_module");
if (!init) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s': js_init_module not found",
module_name);
goto fail;
}

m = init(ctx, module_name);
if (!m) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s': initialization error",
module_name);
fail:
if (hd)
FreeLibrary(hd);
return NULL;
}
return m;
Copy link
Collaborator

@chqrlie chqrlie Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of duplicating the whole function, I would favor using wrappers for the handle declaration, module loading, symbol lookup and module unloading, using macros:

#if defined(_WIN32)
typedef Handle lib_handle;
#define lib_open(name)  LoadLibrary(name)
#define lib_sym(hd, name)  GetProcAddress(hd, name)
#define lib_close(hd) FreeLibrary(hd)
#define lib_extension ".dll"
#else
typedef void *lib_handle;
#define lib_open(name)   dlopen(name, RTLD_NOW | RTLD_LOCAL)
#define lib_sym(hd, name)  dlsym(hd, name)
#define lib_close(hd) dlclose(hd)
#define lib_extension ".so"
#endif

Comment on lines +3877 to +3890
JSValue JS_NewStringWLen(JSContext *ctx, size_t buf_len)
{
JSString *str;
if (buf_len <= 0) {
return JS_AtomToString(ctx, JS_ATOM_empty_string);
}
str = js_alloc_string_rt(ctx->rt, buf_len, 0);
if (unlikely(!str)){
JS_ThrowOutOfMemory(ctx);
return JS_EXCEPTION;
}
memset(str->u.str8, 0, buf_len+1);
return JS_MKPTR(JS_TAG_STRING, str);
}
Copy link
Collaborator

@chqrlie chqrlie Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a new API, I would modify JS_NewStringLen to accept a null pointer and initialize the allocated string with null bytes using this:

    if (buf_len <= 0) {
        return JS_AtomToString(ctx, JS_ATOM_empty_string);
    }
    if (buf == NULL) {
        JSString *str = js_alloc_string(ctx, buf_len, 0);
        if (!str)
            return JS_EXCEPTION;
        memset(str->u.str8, buf, buf_len + 1);
        return JS_MKPTR(JS_TAG_STRING, str);
    }

Comment on lines +3997 to +4008
uint8_t *JS_ToCStringLenRaw(JSContext *ctx, size_t *plen, JSValueConst val)
{
if (JS_VALUE_GET_TAG(val) != JS_TAG_STRING)
{
if(plen) *plen = 0;
return NULL;
}
JSString *str = JS_VALUE_GET_STRING(val);
if(plen) *plen = str->len;
return str->u.str8;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has a problem: the caller cannot determine if the string was encoded as 8-bit or 16-bit. Furthermore the name is misleading: the returned pointer is not UTF-8 encoded if the string has non-ASCII characters.

Comment on lines -4932 to +4958
JSValue JS_NewObjectClass(JSContext *ctx, int class_id)
JSValue JS_NewObjectClass(JSContext *ctx, JSClassID class_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prototype must be changed in quickjs.h too.

@chqrlie
Copy link
Collaborator

chqrlie commented Mar 22, 2024

Hello @zwarrior1,

Thank you for contributing. I cannot merge your patch as posted but will apply your patches with attribution after some adjustments. See my comments inline.

Chqrlie.

@saghul
Copy link
Contributor

saghul commented Mar 22, 2024

I'd love to see this broken down into smaller, focused PRs since the changes don't depend on each other.

@chqrlie
Copy link
Collaborator

chqrlie commented Mar 22, 2024

I'd love to see this broken down into smaller, focused PRs since the changes don't depend on each other.

Of course. But given the change requests, I am going to do this myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants