Commit f2becd0c authored by André Bargull's avatar André Bargull
Browse files

Bug 1492090 - Part 2: Use UTF-8 encoding when printing to stdout and use...

Bug 1492090 - Part 2: Use UTF-8 encoding when printing to stdout and use CompileUtf8Path for xpc-shells. r=nbp

Differential Revision: https://phabricator.services.mozilla.com/D151446
parent e302551e
Loading
Loading
Loading
Loading
+10 −14
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -113,24 +113,20 @@ static bool Load(JSContext* cx, unsigned argc, JS::Value* vp) {

  for (unsigned i = 0; i < args.length(); i++) {
    JS::Rooted<JSString*> str(cx, JS::ToString(cx, args[i]));
    if (!str) return false;
    if (!str) {
      return false;
    }
    JS::UniqueChars filename = JS_EncodeStringToLatin1(cx, str);
    if (!filename) return false;
    FILE* file = fopen(filename.get(), "r");
    if (!file) {
      filename = JS_EncodeStringToUTF8(cx, str);
      if (!filename) return false;
      JS_ReportErrorUTF8(cx, "cannot open file '%s' for reading",
                         filename.get());
    if (!filename) {
      return false;
    }

    JS::CompileOptions options(cx);
    options.setFileAndLine(filename.get(), 1);

    JS::Rooted<JSScript*> script(cx, JS::CompileUtf8File(cx, options, file));
    fclose(file);
    if (!script) return false;
    JS::Rooted<JSScript*> script(
        cx, JS::CompileUtf8Path(cx, options, filename.get()));
    if (!script) {
      return false;
    }

    if (!JS_ExecuteScript(cx, script)) {
      return false;
+1 −1
Original line number Diff line number Diff line
@@ -3727,7 +3727,7 @@ bool IterativeFailureTest::testThread(unsigned thread) {
        fprintf(stderr, "  error while trying to print exception, giving up\n");
        return false;
      }
      UniqueChars bytes(JS_EncodeStringToLatin1(cx, str));
      UniqueChars bytes(JS_EncodeStringToUTF8(cx, str));
      if (!bytes) {
        return false;
      }
+7 −5
Original line number Diff line number Diff line
@@ -168,8 +168,9 @@ class JSAPIRuntimeTest : public JSAPITest {
                JS::MutableHandleValue vp);

  JSAPITestString jsvalToSource(JS::HandleValue v) {
    if (JSString* str = JS_ValueToSource(cx, v)) {
      if (JS::UniqueChars bytes = JS_EncodeStringToLatin1(cx, str)) {
    JS::Rooted<JSString*> str(cx, JS_ValueToSource(cx, v));
    if (str) {
      if (JS::UniqueChars bytes = JS_EncodeStringToUTF8(cx, str)) {
        return JSAPITestString(bytes.get());
      }
    }
@@ -328,7 +329,7 @@ class JSAPIRuntimeTest : public JSAPITest {
      JS::RootedValue v(cx);
      JS_GetPendingException(cx, &v);
      JS_ClearPendingException(cx);
      JSString* s = JS::ToString(cx, v);
      JS::Rooted<JSString*> s(cx, JS::ToString(cx, v));
      if (s) {
        if (JS::UniqueChars bytes = JS_EncodeStringToLatin1(cx, s)) {
          message += bytes.get();
@@ -347,12 +348,13 @@ class JSAPIRuntimeTest : public JSAPITest {
  static bool print(JSContext* cx, unsigned argc, JS::Value* vp) {
    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);

    JS::Rooted<JSString*> str(cx);
    for (unsigned i = 0; i < args.length(); i++) {
      JSString* str = JS::ToString(cx, args[i]);
      str = JS::ToString(cx, args[i]);
      if (!str) {
        return false;
      }
      JS::UniqueChars bytes = JS_EncodeStringToLatin1(cx, str);
      JS::UniqueChars bytes = JS_EncodeStringToUTF8(cx, str);
      if (!bytes) {
        return false;
      }
+3 −17
Original line number Diff line number Diff line
@@ -379,24 +379,10 @@ static bool Load(JSContext* cx, unsigned argc, Value* vp) {
    if (!filename) {
      return false;
    }
    FILE* file = fopen(filename.get(), "r");
    if (!file) {
      filename = JS_EncodeStringToUTF8(cx, str);
      if (!filename) {
        return false;
      }
      JS_ReportErrorUTF8(cx, "cannot open file '%s' for reading",
                         filename.get());
      return false;
    }
    JS::CompileOptions options(cx);
    options.setFileAndLine(filename.get(), 1)
        .setIsRunOnce(true)
        .setSkipFilenameValidation(true);
    JS::Rooted<JSScript*> script(cx);
    JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
    script = JS::CompileUtf8File(cx, options, file);
    fclose(file);
    options.setIsRunOnce(true).setSkipFilenameValidation(true);
    JS::Rooted<JSScript*> script(
        cx, JS::CompileUtf8Path(cx, options, filename.get()));
    if (!script) {
      return false;
    }
+4 −1
Original line number Diff line number Diff line
@@ -14,7 +14,10 @@ function run_test() {
    load("file_that_does_not_exist.js");
    subscriptLoaded = true;
  } catch (ex) {
    Assert.equal(ex.message.substring(0, 16), "cannot open file");
    Assert.ok(
      ex.message.startsWith("can't open "),
      `Unexpected message: ${ex.message}`
    );
  }
  Assert.ok(!subscriptLoaded, "load() should throw an error");
}