Skip to content

Commit 34b725d

Browse files
harumazzzFabrice Bellard
andauthored
Add JS_AtomToCStringLen (#1071)
Cherry-pick of bellard/quickjs@8381245. Co-authored-by: Fabrice Bellard <[email protected]> Co-authored-by: Haruma <[email protected]>
1 parent 5be76a7 commit 34b725d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

quickjs.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,15 +3459,18 @@ static __maybe_unused void print_atom(JSContext *ctx, JSAtom atom)
34593459
}
34603460

34613461
/* free with JS_FreeCString() */
3462-
const char *JS_AtomToCString(JSContext *ctx, JSAtom atom)
3462+
const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom)
34633463
{
34643464
JSValue str;
34653465
const char *cstr;
34663466

34673467
str = JS_AtomToString(ctx, atom);
3468-
if (JS_IsException(str))
3468+
if (JS_IsException(str)) {
3469+
if (plen)
3470+
*plen = 0;
34693471
return NULL;
3470-
cstr = JS_ToCString(ctx, str);
3472+
}
3473+
cstr = JS_ToCStringLen(ctx, plen, str);
34713474
JS_FreeValue(ctx, str);
34723475
return cstr;
34733476
}

quickjs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,11 @@ JS_EXTERN void JS_FreeAtom(JSContext *ctx, JSAtom v);
544544
JS_EXTERN void JS_FreeAtomRT(JSRuntime *rt, JSAtom v);
545545
JS_EXTERN JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom);
546546
JS_EXTERN JSValue JS_AtomToString(JSContext *ctx, JSAtom atom);
547-
JS_EXTERN const char *JS_AtomToCString(JSContext *ctx, JSAtom atom);
547+
JS_EXTERN const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom);
548+
static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom)
549+
{
550+
return JS_AtomToCStringLen(ctx, NULL, atom);
551+
}
548552
JS_EXTERN JSAtom JS_ValueToAtom(JSContext *ctx, JSValueConst val);
549553

550554
/* object class support */

0 commit comments

Comments
 (0)