示例#1
0
static bool
Quote(StringBuffer& sb, JSLinearString* str)
{
    size_t len = str->length();

    /* Step 1. */
    if (!sb.append('"'))
        return false;

    /* Step 2. */
    JS::AutoCheckCannotGC nogc;
    const RangedPtr<const CharT> buf(str->chars<CharT>(nogc), len);
    for (size_t i = 0; i < len; ++i) {
        /* Batch-append maximal character sequences containing no escapes. */
        size_t mark = i;
        do {
            if (IsQuoteSpecialCharacter(buf[i]))
                break;
        } while (++i < len);
        if (i > mark) {
            if (!sb.appendSubstring(str, mark, i - mark))
                return false;
            if (i == len)
                break;
        }

        char16_t c = buf[i];
        if (c == '"' || c == '\\') {
            if (!sb.append('\\') || !sb.append(c))
                return false;
        } else if (c == '\b' || c == '\f' || c == '\n' || c == '\r' || c == '\t') {
           char16_t abbrev = (c == '\b')
                         ? 'b'
                         : (c == '\f')
                         ? 'f'
                         : (c == '\n')
                         ? 'n'
                         : (c == '\r')
                         ? 'r'
                         : 't';
           if (!sb.append('\\') || !sb.append(abbrev))
               return false;
        } else {
            MOZ_ASSERT(c < ' ');
            if (!sb.append("\\u00"))
                return false;
            MOZ_ASSERT((c >> 4) < 10);
            uint8_t x = c >> 4, y = c % 16;
            if (!sb.append(Latin1Char('0' + x)) ||
                !sb.append(Latin1Char(y < 10 ? '0' + y : 'a' + (y - 10))))
            {
                return false;
            }
        }
    }

    /* Steps 3-4. */
    return sb.append('"');
}
示例#2
0
static bool
AppendUseStrictSource(JSContext *cx, HandleFunction fun, Handle<JSFlatString*> src, StringBuffer &out)
{
    // We need to add "use strict" in the body right after the opening
    // brace.
    size_t bodyStart = 0, bodyEnd;

    // No need to test for functions created with the Function ctor as
    // these don't implicitly inherit the "use strict" context. Strict mode is
    // enabled for functions created with the Function ctor only if they begin with
    // the "use strict" directive, but these functions won't validate as asm.js
    // modules.

    if (!FindBody(cx, fun, src, &bodyStart, &bodyEnd))
        return false;

    return out.appendSubstring(src, 0, bodyStart) &&
           out.append("\n\"use strict\";\n") &&
           out.appendSubstring(src, bodyStart, src->length() - bodyStart);
}