-
Notifications
You must be signed in to change notification settings - Fork 984
Small fixes and additional functions #252
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3874,6 +3874,20 @@ static JSValue string_buffer_end(StringBuffer *s) | |
return JS_MKPTR(JS_TAG_STRING, str); | ||
} | ||
|
||
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); | ||
} | ||
Comment on lines
+3877
to
+3890
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of a new API, I would modify
|
||
/* create a string from a UTF-8 buffer */ | ||
JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len) | ||
{ | ||
|
@@ -3980,6 +3994,18 @@ JSValue JS_NewAtomString(JSContext *ctx, const char *str) | |
return val; | ||
} | ||
|
||
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; | ||
} | ||
|
||
Comment on lines
+3997
to
+4008
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
/* return (NULL, 0) if exception. */ | ||
/* return pointer into a JSString with a live ref_count */ | ||
/* cesu8 determines if non-BMP1 codepoints are encoded as 1 or 2 utf-8 sequences */ | ||
|
@@ -4929,7 +4955,7 @@ static int JS_SetObjectData(JSContext *ctx, JSValueConst obj, JSValue val) | |
return -1; | ||
} | ||
|
||
JSValue JS_NewObjectClass(JSContext *ctx, int class_id) | ||
JSValue JS_NewObjectClass(JSContext *ctx, JSClassID class_id) | ||
Comment on lines
-4932
to
+4958
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The prototype must be changed in quickjs.h too. |
||
{ | ||
return JS_NewObjectProtoClass(ctx, ctx->class_proto[class_id], class_id); | ||
} | ||
|
@@ -43229,11 +43255,10 @@ static uint64_t xorshift64star(uint64_t *pstate) | |
return x * 0x2545F4914F6CDD1D; | ||
} | ||
|
||
static int64_t date_now(void); | ||
static void js_random_init(JSContext *ctx) | ||
{ | ||
struct timeval tv; | ||
gettimeofday(&tv, NULL); | ||
ctx->random_state = ((int64_t)tv.tv_sec * 1000000) + tv.tv_usec; | ||
ctx->random_state = date_now(); | ||
/* the state must be non zero */ | ||
if (ctx->random_state == 0) | ||
ctx->random_state = 1; | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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: