1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testScriptObject.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,157 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include "jsapi-tests/tests.h" 1.12 + 1.13 +struct ScriptObjectFixture : public JSAPITest { 1.14 + static const int code_size; 1.15 + static const char code[]; 1.16 + static jschar uc_code[]; 1.17 + 1.18 + ScriptObjectFixture() 1.19 + { 1.20 + for (int i = 0; i < code_size; i++) 1.21 + uc_code[i] = code[i]; 1.22 + } 1.23 + 1.24 + bool tryScript(JS::HandleObject global, JSScript *scriptArg) 1.25 + { 1.26 + JS::RootedScript script(cx, scriptArg); 1.27 + CHECK(script); 1.28 + 1.29 + JS_GC(rt); 1.30 + 1.31 + /* After a garbage collection, the script should still work. */ 1.32 + JS::RootedValue result(cx); 1.33 + CHECK(JS_ExecuteScript(cx, global, script, &result)); 1.34 + 1.35 + return true; 1.36 + } 1.37 +}; 1.38 + 1.39 +const char ScriptObjectFixture::code[] = 1.40 + "(function(a, b){return a+' '+b;}('hello', 'world'))"; 1.41 +const int ScriptObjectFixture::code_size = sizeof(ScriptObjectFixture::code) - 1; 1.42 +jschar ScriptObjectFixture::uc_code[ScriptObjectFixture::code_size]; 1.43 + 1.44 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript) 1.45 +{ 1.46 + JS::CompileOptions options(cx); 1.47 + options.setFileAndLine(__FILE__, __LINE__); 1.48 + return tryScript(global, JS_CompileScript(cx, global, code, code_size, 1.49 + options)); 1.50 +} 1.51 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript) 1.52 + 1.53 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript_empty) 1.54 +{ 1.55 + JS::CompileOptions options(cx); 1.56 + options.setFileAndLine(__FILE__, __LINE__); 1.57 + return tryScript(global, JS_CompileScript(cx, global, "", 0, options)); 1.58 +} 1.59 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript_empty) 1.60 + 1.61 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScriptForPrincipals) 1.62 +{ 1.63 + JS::CompileOptions options(cx); 1.64 + options.setFileAndLine(__FILE__, __LINE__); 1.65 + return tryScript(global, JS_CompileScript(cx, global, code, code_size, 1.66 + options)); 1.67 +} 1.68 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScriptForPrincipals) 1.69 + 1.70 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) 1.71 +{ 1.72 + JS::CompileOptions options(cx); 1.73 + options.setFileAndLine(__FILE__, __LINE__); 1.74 + return tryScript(global, JS_CompileUCScript(cx, global, uc_code, code_size, 1.75 + options)); 1.76 +} 1.77 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) 1.78 + 1.79 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) 1.80 +{ 1.81 + JS::CompileOptions options(cx); 1.82 + options.setFileAndLine(__FILE__, __LINE__); 1.83 + return tryScript(global, JS_CompileUCScript(cx, global, uc_code, 0, 1.84 + options)); 1.85 +} 1.86 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) 1.87 + 1.88 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipals) 1.89 +{ 1.90 + JS::CompileOptions options(cx); 1.91 + options.setFileAndLine(__FILE__, __LINE__); 1.92 + return tryScript(global, JS_CompileUCScript(cx, global, uc_code, code_size, 1.93 + options)); 1.94 +} 1.95 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipals) 1.96 + 1.97 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile) 1.98 +{ 1.99 + TempFile tempScript; 1.100 + static const char script_filename[] = "temp-bug438633_JS_CompileFile"; 1.101 + FILE *script_stream = tempScript.open(script_filename); 1.102 + CHECK(fputs(code, script_stream) != EOF); 1.103 + tempScript.close(); 1.104 + JS::CompileOptions options(cx); 1.105 + options.setFileAndLine(script_filename, 1); 1.106 + JSScript *script = JS::Compile(cx, global, options, script_filename); 1.107 + tempScript.remove(); 1.108 + return tryScript(global, script); 1.109 +} 1.110 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile) 1.111 + 1.112 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile_empty) 1.113 +{ 1.114 + TempFile tempScript; 1.115 + static const char script_filename[] = "temp-bug438633_JS_CompileFile_empty"; 1.116 + tempScript.open(script_filename); 1.117 + tempScript.close(); 1.118 + JS::CompileOptions options(cx); 1.119 + options.setFileAndLine(script_filename, 1); 1.120 + JSScript *script = JS::Compile(cx, global, options, script_filename); 1.121 + tempScript.remove(); 1.122 + return tryScript(global, script); 1.123 +} 1.124 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFile_empty) 1.125 + 1.126 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle) 1.127 +{ 1.128 + const char *script_filename = "temporary file"; 1.129 + TempFile tempScript; 1.130 + FILE *script_stream = tempScript.open("temp-bug438633_JS_CompileFileHandle"); 1.131 + CHECK(fputs(code, script_stream) != EOF); 1.132 + CHECK(fseek(script_stream, 0, SEEK_SET) != EOF); 1.133 + JS::CompileOptions options(cx); 1.134 + options.setFileAndLine(script_filename, 1); 1.135 + return tryScript(global, JS::Compile(cx, global, options, script_stream)); 1.136 +} 1.137 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle) 1.138 + 1.139 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle_empty) 1.140 +{ 1.141 + const char *script_filename = "empty temporary file"; 1.142 + TempFile tempScript; 1.143 + FILE *script_stream = tempScript.open("temp-bug438633_JS_CompileFileHandle_empty"); 1.144 + JS::CompileOptions options(cx); 1.145 + options.setFileAndLine(script_filename, 1); 1.146 + return tryScript(global, JS::Compile(cx, global, options, script_stream)); 1.147 +} 1.148 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandle_empty) 1.149 + 1.150 +BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandleForPrincipals) 1.151 +{ 1.152 + TempFile tempScript; 1.153 + FILE *script_stream = tempScript.open("temp-bug438633_JS_CompileFileHandleForPrincipals"); 1.154 + CHECK(fputs(code, script_stream) != EOF); 1.155 + CHECK(fseek(script_stream, 0, SEEK_SET) != EOF); 1.156 + JS::CompileOptions options(cx); 1.157 + options.setFileAndLine("temporary file", 1); 1.158 + return tryScript(global, JS::Compile(cx, global, options, script_stream)); 1.159 +} 1.160 +END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandleForPrincipals)