Bug 1013917 part 2 - Add shell functions for testing latin1 strings. r=luke

This commit is contained in:
Jan de Mooij 2014-05-22 21:40:36 +02:00
parent 5096a71998
commit aa5e6777ec
4 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,5 @@
var s = "Foo123";
assertEq(isLatin1(s), false);
s = toLatin1(s);
assertEq(isLatin1(s), true);

View File

@ -4430,6 +4430,30 @@ PrintProfilerEvents(JSContext *cx, unsigned argc, Value *vp)
return true;
}
static bool
IsLatin1(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
bool isLatin1 = args.get(0).isString() && args[0].toString()->hasLatin1Chars();
args.rval().setBoolean(isLatin1);
return true;
}
static bool
ToLatin1(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (!args.get(0).isString() || !args[0].toString()->isLinear()) {
args.rval().setUndefined();
return true;
}
JSLinearString *s = &args[0].toString()->asLinear();
s->debugUnsafeConvertToLatin1();
args.rval().setString(s);
return true;
}
static const JSFunctionSpecWithHelp shell_functions[] = {
JS_FN_HELP("version", Version, 0, 0,
"version([number])",
@ -4794,6 +4818,10 @@ static const JSFunctionSpecWithHelp shell_functions[] = {
" Register a callback with the profiler that prints javascript profiler events\n"
" to stderr. Callback is only registered if profiling is enabled."),
JS_FN_HELP("isLatin1", IsLatin1, 1, 0,
"isLatin1(s)",
" Return true iff the string's characters are stored as Latin1."),
JS_FS_HELP_END
};
@ -4858,6 +4886,10 @@ static const JSFunctionSpecWithHelp fuzzing_unsafe_functions[] = {
"untrap(fun[, pc])",
" Remove a trap."),
JS_FN_HELP("toLatin1", ToLatin1, 1, 0,
"toLatin1(s)",
" Convert the string's characters to Latin1."),
JS_FN_HELP("withSourceHook", WithSourceHook, 1, 0,
"withSourceHook(hook, fun)",
" Set this JS runtime's lazy source retrieval hook (that is, the hook\n"

View File

@ -116,6 +116,27 @@ JSString::equals(const char *s)
}
#endif /* DEBUG */
void
JSLinearString::debugUnsafeConvertToLatin1()
{
// Temporary helper function to test changes for bug 998392.
MOZ_ASSERT(hasTwoByteChars());
MOZ_ASSERT(!hasBase());
size_t len = length();
const jschar *twoByteChars = chars();
char *latin1Chars = (char *)twoByteChars;
for (size_t i = 0; i < len; i++) {
MOZ_ASSERT((twoByteChars[i] & 0xff00) == 0);
latin1Chars[i] = char(twoByteChars[i]);
}
latin1Chars[len] = '\0';
d.u1.flags |= LATIN1_CHARS_BIT;
}
static MOZ_ALWAYS_INLINE bool
AllocChars(ThreadSafeContext *maybecx, size_t length, jschar **chars, size_t *capacity)
{

View File

@ -592,6 +592,9 @@ class JSLinearString : public JSString
JS_ASSERT(JSString::isLinear());
return JS::TwoByteChars(chars(), length());
}
/* Temporary, unsafe helper function for bug 998392. Don't use for anything else. */
void debugUnsafeConvertToLatin1();
};
JS_STATIC_ASSERT(sizeof(JSLinearString) == sizeof(JSString));