├── .gitignore ├── LICENSE ├── README.md ├── win10 ├── edge │ ├── jsrt-wrappers.sln │ ├── src │ │ ├── jsrt-wrappers.cpp │ │ ├── jsrt-wrappers.h │ │ ├── jsrt-wrappers.vcxproj │ │ ├── jsrt-wrappers.vcxproj.filters │ │ ├── stdafx.cpp │ │ ├── stdafx.h │ │ └── targetver.h │ └── test │ │ ├── array.cpp │ │ ├── array_buffer.cpp │ │ ├── bound_function.cpp │ │ ├── context.cpp │ │ ├── data_view.cpp │ │ ├── error.cpp │ │ ├── exceptions.cpp │ │ ├── function.cpp │ │ ├── jsrt-wrappers-test.vcxproj │ │ ├── jsrt-wrappers-test.vcxproj.filters │ │ ├── object.cpp │ │ ├── optional.cpp │ │ ├── pinned.cpp │ │ ├── profiler.h │ │ ├── property_descriptor.cpp │ │ ├── property_id.cpp │ │ ├── readme.cpp │ │ ├── reference.cpp │ │ ├── runtime.cpp │ │ ├── stdafx.cpp │ │ ├── stdafx.h │ │ ├── symbol.cpp │ │ ├── targetver.h │ │ ├── typed_array.cpp │ │ └── value.cpp └── ie │ ├── jsrt-wrappers.sln │ ├── src │ ├── jsrt-wrappers.cpp │ ├── jsrt-wrappers.h │ ├── jsrt-wrappers.vcxproj │ ├── jsrt-wrappers.vcxproj.filters │ ├── stdafx.cpp │ ├── stdafx.h │ └── targetver.h │ └── test │ ├── array.cpp │ ├── bound_function.cpp │ ├── context.cpp │ ├── error.cpp │ ├── exceptions.cpp │ ├── function.cpp │ ├── jsrt-wrappers-test.vcxproj │ ├── jsrt-wrappers-test.vcxproj.filters │ ├── object.cpp │ ├── optional.cpp │ ├── pinned.cpp │ ├── profiler.h │ ├── property_descriptor.cpp │ ├── property_id.cpp │ ├── readme.cpp │ ├── reference.cpp │ ├── runtime.cpp │ ├── stdafx.cpp │ ├── stdafx.h │ ├── targetver.h │ └── value.cpp └── win8.1 ├── jsrt-wrappers.sln ├── src ├── jsrt-wrappers.cpp ├── jsrt-wrappers.h ├── jsrt-wrappers.vcxproj ├── jsrt-wrappers.vcxproj.filters ├── stdafx.cpp ├── stdafx.h └── targetver.h └── test ├── array.cpp ├── bound_function.cpp ├── context.cpp ├── error.cpp ├── exceptions.cpp ├── function.cpp ├── jsrt-wrappers-test.vcxproj ├── jsrt-wrappers-test.vcxproj.filters ├── object.cpp ├── optional.cpp ├── pinned.cpp ├── profiler.h ├── property_descriptor.cpp ├── property_id.cpp ├── readme.cpp ├── reference.cpp ├── runtime.cpp ├── stdafx.cpp ├── stdafx.h ├── targetver.h └── value.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore built files 2 | *.opensdf 3 | *.sdf 4 | *.suo 5 | *.user 6 | win8.1/ipch/ 7 | win8.1/Debug/ 8 | win8.1/Release/ 9 | win8.1/x64/ 10 | win8.1/src/Debug/ 11 | win8.1/src/Release/ 12 | win8.1/src/x64/ 13 | win8.1/test/Debug/ 14 | win8.1/test/Release/ 15 | win8.1/test/x64/ 16 | win10/ie/ipch/ 17 | win10/ie/Debug/ 18 | win10/ie/Release/ 19 | win10/ie/x64/ 20 | win10/ie/src/Debug/ 21 | win10/ie/src/Release/ 22 | win10/ie/src/x64/ 23 | win10/ie/test/Debug/ 24 | win10/ie/test/Release/ 25 | win10/ie/test/x64/ 26 | win10/edge/ipch/ 27 | win10/edge/Debug/ 28 | win10/edge/Release/ 29 | win10/edge/x64/ 30 | win10/edge/src/Debug/ 31 | win10/edge/src/Release/ 32 | win10/edge/src/x64/ 33 | win10/edge/test/Debug/ 34 | win10/edge/test/Release/ 35 | win10/edge/test/x64/ 36 | *.opendb 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #JsRT Wrappers for C++ 2 | 3 | The JsRT Wrappers C++ are intended to simplify working with the JsRT interface to the Chakra JavaScript engine in C++. Since the JsRT API is a flat Win32-style interface, there are a number of conventions it uses that are not entirely friendly to the C++ programmer. This library tries to smooth that out as much as possible. 4 | 5 | ## Install 6 | 7 | Simply download the latest release and unzip. `#include` the `jsrt-wrapper.h` header and link in `jsrt-wrapper.lib` for the relevant platform. 8 | 9 | ## Dependencies 10 | 11 | The JsRT APIs are only available on Windows 8.1 or later. 12 | 13 | ## A Tour Through the Helpers 14 | 15 | Most of the wrappers simply provide a nice object-oriented interface over the JsRT API. For example, creating a runtime and a context works like this: 16 | 17 | jsrt::runtime runtime = jsrt::runtime::create(); 18 | jsrt::context context = runtime.create_context(); 19 | 20 | There are, however, a number of special features in the wrappers. 21 | 22 | ### Context scopes 23 | 24 | A context scope (`jsrt::context::scope`) automatically sets the current context when it comes into scope and clears the current context when it goes out of scope. This greatly simplifies working with scopes. For example: 25 | 26 | jsrt::runtime runtime = jsrt::runtime::create(); 27 | jsrt::context context = runtime.create_context(); 28 | { 29 | jsrt::context::scope scope(context); 30 | 31 | ... work with context ... 32 | } 33 | // Context has automatically been set back to original value. 34 | runtime.dispose(); 35 | 36 | ### Pinned references 37 | 38 | To keep a reference to a Chakra object alive, it must be visible to the Chakra garbage collector (i.e. on the stack or stored in the garbage collected heap) or it has to be pinned using `JsAddRef`. The `pinned` class functions as a smart-pointer template that keeps a reference alive using `JsAddRef` and `JsRelease` automatically. For example: 39 | 40 | { 41 | jsrt::pinned pinned_obj = jsrt::object::create(); 42 | // Object reference does not have to stay on the stack or in the GC heap 43 | } 44 | // Reference can now be garbage collected 45 | 46 | ### Value translation 47 | 48 | Value getter and setter functions can be strongly-typed, allowing automatic marshalling of JavaScript values to C++ values and vice versa. For example: 49 | 50 | jsrt::object obj = jsrt::object::create(); 51 | obj.set_property(jsrt::property_id::create(L"boolProperty"), true); 52 | bool bool_value = obj.get_property(jsrt::property_id::create(L"boolProperty")); 53 | obj.set_property(jsrt::property_id::create(L"stringProperty"), L"foo"); 54 | 55 | The wrappers marshal types in the following way: 56 | 57 | * number values are marshalled to/from `double` 58 | * string values are marshalled to/from `std::wstring` 59 | * Boolean values are marshalled to/from `bool` 60 | 61 | ### Strongly-typed arrays 62 | 63 | Similarly, arrays can be strongly typed and accessed using numeric indexes: 64 | 65 | jsrt::array darray = jsrt::array::create(1); 66 | darray[0] = 10; 67 | darray[1] = 20; 68 | 69 | ### Functions 70 | 71 | Perhaps the most useful aspect of the wrappers is the way that JavaScript functions can be wrapped and their arguments and return values marshalled to C++ types, and vice versa. For example: 72 | 73 | auto f = (jsrt::function)jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;"); 74 | double a = f(jsrt::context::undefined(), 1, 2); 75 | 76 | Native functions can also be wrapped: 77 | 78 | double add(const jsrt::call_info &info, double a, double b) { return a + b; } 79 | auto nf = jsrt::function::create(add); 80 | jsrt::context::global().set_property(jsrt::property_id::create(L"add"), nf); 81 | jsrt::context::run(L"add(1, 2)"); 82 | 83 | When calling a function wrapper, the first argument to the function call is the `this` value for the call. Function wrappers can also be _bound_ to a particular value of `this`. For example: 84 | 85 | auto bf = jsrt::bound_function::create( 86 | jsrt::context::undefined(), 87 | (jsrt::function)jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;")); 88 | double ba = bf(1, 2); 89 | -------------------------------------------------------------------------------- /win10/edge/jsrt-wrappers.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30110.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsrt-wrappers", "src\jsrt-wrappers.vcxproj", "{18B6A46C-9534-498D-8463-9822BC5CAC42}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsrt-wrappers-test", "test\jsrt-wrappers-test.vcxproj", "{3E7E9985-7F51-49BC-9B1F-794D27F860DB}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Win32 = Debug|Win32 13 | Debug|x64 = Debug|x64 14 | Release|Win32 = Release|Win32 15 | Release|x64 = Release|x64 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|Win32.Build.0 = Debug|Win32 20 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|x64.ActiveCfg = Debug|x64 21 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|x64.Build.0 = Debug|x64 22 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|Win32.ActiveCfg = Release|Win32 23 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|Win32.Build.0 = Release|Win32 24 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|x64.ActiveCfg = Release|x64 25 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|x64.Build.0 = Release|x64 26 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|Win32.ActiveCfg = Debug|Win32 27 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|Win32.Build.0 = Debug|Win32 28 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|x64.ActiveCfg = Debug|x64 29 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|x64.Build.0 = Debug|x64 30 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|Win32.ActiveCfg = Release|Win32 31 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|Win32.Build.0 = Release|Win32 32 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|x64.ActiveCfg = Release|x64 33 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|x64.Build.0 = Release|x64 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | EndGlobal 39 | -------------------------------------------------------------------------------- /win10/edge/src/jsrt-wrappers.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {18B6A46C-9534-498D-8463-9822BC5CAC42} 23 | Win32Proj 24 | jsrtwrappers 25 | 10.0.10586.0 26 | 27 | 28 | 29 | StaticLibrary 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | StaticLibrary 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | 43 | 44 | Use 45 | Level3 46 | Disabled 47 | USE_EDGEMODE_JSRT;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 48 | true 49 | true 50 | 51 | 52 | Windows 53 | true 54 | 55 | 56 | true 57 | 58 | 59 | 60 | 61 | Level3 62 | Use 63 | MaxSpeed 64 | true 65 | true 66 | USE_EDGEMODE_JSRT;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 67 | true 68 | 69 | 70 | Windows 71 | true 72 | true 73 | true 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Create 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /win10/edge/src/jsrt-wrappers.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | -------------------------------------------------------------------------------- /win10/edge/src/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | -------------------------------------------------------------------------------- /win10/edge/src/stdafx.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "targetver.h" 18 | #define WIN32_LEAN_AND_MEAN 19 | #include "jsrt.h" -------------------------------------------------------------------------------- /win10/edge/src/targetver.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | -------------------------------------------------------------------------------- /win10/edge/test/array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(array) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty array handle.") 26 | { 27 | jsrt::array<> handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::array<> array; 37 | TEST_NO_CONTEXT_CALL(jsrt::array<>::create(0)); 38 | { 39 | jsrt::context::scope scope(context); 40 | array = jsrt::array<>::create(1); 41 | } 42 | TEST_NO_CONTEXT_CALL(array.size()); 43 | TEST_NO_CONTEXT_CALL(static_cast(array[0])); 44 | TEST_NO_CONTEXT_CALL(array[0] = jsrt::value()); 45 | runtime.dispose(); 46 | } 47 | 48 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 49 | { 50 | jsrt::runtime runtime = jsrt::runtime::create(); 51 | jsrt::context context = runtime.create_context(); 52 | { 53 | jsrt::context::scope scope(context); 54 | jsrt::array<> array; 55 | TEST_INVALID_ARG_CALL(array.size()); 56 | TEST_INVALID_ARG_CALL(static_cast(array[0])); 57 | TEST_INVALID_ARG_CALL(array[0] = jsrt::value()); 58 | } 59 | runtime.dispose(); 60 | } 61 | 62 | MY_TEST_METHOD(create, "Test ::create method.") 63 | { 64 | jsrt::runtime runtime = jsrt::runtime::create(); 65 | jsrt::context context = runtime.create_context(); 66 | { 67 | jsrt::context::scope scope(context); 68 | jsrt::value value = jsrt::array<>::create(0); 69 | static_cast>(value); 70 | 71 | jsrt::array array2 = jsrt::array::create({ 1, 2, 3, 4 }); 72 | Assert::AreEqual(static_cast(array2[0]), 1.0); 73 | Assert::AreEqual(static_cast(array2[3]), 4.0); 74 | } 75 | runtime.dispose(); 76 | } 77 | 78 | MY_TEST_METHOD(indexing, "Test indexing.") 79 | { 80 | jsrt::runtime runtime = jsrt::runtime::create(); 81 | jsrt::context context = runtime.create_context(); 82 | { 83 | jsrt::context::scope scope(context); 84 | jsrt::array darray = jsrt::array::create(1); 85 | darray[0] = 10; 86 | darray[1] = 20; 87 | Assert::AreEqual(static_cast(darray[0]), 10.0); 88 | Assert::AreEqual(static_cast(darray[1]), 20.0); 89 | 90 | jsrt::array barray = jsrt::array::create(1); 91 | barray[0] = true; 92 | barray[1] = true; 93 | Assert::IsTrue(static_cast(barray[0])); 94 | Assert::IsTrue(static_cast(barray[1])); 95 | 96 | jsrt::array sarray = jsrt::array::create(1); 97 | sarray[0] = L"foo"; 98 | sarray[1] = L"bar"; 99 | Assert::AreEqual(static_cast(sarray[0]), static_cast(L"foo")); 100 | Assert::AreEqual(static_cast(sarray[1]), static_cast(L"bar")); 101 | 102 | Assert::AreEqual(sarray.size(), 2); 103 | } 104 | runtime.dispose(); 105 | } 106 | }; 107 | } -------------------------------------------------------------------------------- /win10/edge/test/array_buffer.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(array_buffer) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty array_buffer handle.") 26 | { 27 | jsrt::array_buffer handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::array_buffer array; 37 | TEST_NO_CONTEXT_CALL(jsrt::array_buffer::create(0)); 38 | { 39 | jsrt::context::scope scope(context); 40 | array = jsrt::array_buffer::create(0); 41 | } 42 | array.data(); 43 | array.size(); 44 | runtime.dispose(); 45 | } 46 | 47 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 48 | { 49 | jsrt::runtime runtime = jsrt::runtime::create(); 50 | jsrt::context context = runtime.create_context(); 51 | { 52 | jsrt::context::scope scope(context); 53 | jsrt::array_buffer array; 54 | TEST_INVALID_ARG_CALL(array.size()); 55 | TEST_INVALID_ARG_CALL(array.data()); 56 | } 57 | runtime.dispose(); 58 | } 59 | 60 | MY_TEST_METHOD(create, "Test ::create method.") 61 | { 62 | jsrt::runtime runtime = jsrt::runtime::create(); 63 | jsrt::context context = runtime.create_context(); 64 | { 65 | jsrt::context::scope scope(context); 66 | jsrt::value value = jsrt::array_buffer::create(0); 67 | static_cast(value); 68 | } 69 | runtime.dispose(); 70 | } 71 | 72 | MY_TEST_METHOD(data, "Test data and size.") 73 | { 74 | jsrt::runtime runtime = jsrt::runtime::create(); 75 | jsrt::context context = runtime.create_context(); 76 | { 77 | jsrt::context::scope scope(context); 78 | jsrt::array_buffer array = jsrt::array_buffer::create(1); 79 | Assert::AreEqual(array.size(), 1u); 80 | Assert::IsNotNull(array.data()); 81 | } 82 | runtime.dispose(); 83 | } 84 | }; 85 | } -------------------------------------------------------------------------------- /win10/edge/test/data_view.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(data_view) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty data_view handle.") 26 | { 27 | jsrt::data_view<> handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::array_buffer buffer; 37 | { 38 | jsrt::context::scope scope(context); 39 | buffer = jsrt::array_buffer::create(256); 40 | } 41 | jsrt::data_view<> view; 42 | 43 | // JsCreateDataView 44 | TEST_NO_CONTEXT_CALL(jsrt::data_view<>::create(buffer, 0, 0)); 45 | { 46 | jsrt::context::scope scope(context); 47 | view = jsrt::data_view<>::create(buffer, 0, 0); 48 | } 49 | 50 | // JsGetDataViewStorage doesn't require a context 51 | view.data(); 52 | view.size(); 53 | runtime.dispose(); 54 | } 55 | 56 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 57 | { 58 | jsrt::runtime runtime = jsrt::runtime::create(); 59 | jsrt::context context = runtime.create_context(); 60 | { 61 | jsrt::context::scope scope(context); 62 | jsrt::data_view<> view; 63 | TEST_INVALID_ARG_CALL(view.size()); 64 | TEST_INVALID_ARG_CALL(view.data()); 65 | } 66 | runtime.dispose(); 67 | } 68 | 69 | MY_TEST_METHOD(create, "Test ::create method.") 70 | { 71 | jsrt::runtime runtime = jsrt::runtime::create(); 72 | jsrt::context context = runtime.create_context(); 73 | { 74 | jsrt::context::scope scope(context); 75 | jsrt::array_buffer buffer = jsrt::array_buffer::create(0); 76 | jsrt::value value = jsrt::data_view<>::create(buffer, 0, 0); 77 | static_cast>(value); 78 | } 79 | runtime.dispose(); 80 | } 81 | 82 | MY_TEST_METHOD(data, "Test data and size.") 83 | { 84 | jsrt::runtime runtime = jsrt::runtime::create(); 85 | jsrt::context context = runtime.create_context(); 86 | { 87 | jsrt::context::scope scope(context); 88 | jsrt::array_buffer buffer = jsrt::array_buffer::create(20); 89 | jsrt::data_view<> view = jsrt::data_view<>::create(buffer, 5, 10); 90 | Assert::AreEqual(view.size(), 10u); 91 | Assert::IsNotNull(view.data()); 92 | } 93 | runtime.dispose(); 94 | } 95 | 96 | MY_TEST_METHOD(accessors, "Test get and set methods.") 97 | { 98 | jsrt::runtime runtime = jsrt::runtime::create(); 99 | jsrt::context context = runtime.create_context(); 100 | { 101 | jsrt::context::scope scope(context); 102 | jsrt::array_buffer buffer = jsrt::array_buffer::create(12); 103 | jsrt::data_view<> view = jsrt::data_view<>::create(buffer); 104 | view.set(0, 42); 105 | Assert::AreEqual(view.get(0), 42); 106 | view.set(4, 42.0); 107 | Assert::AreEqual(view.get(4), 42.0); 108 | } 109 | runtime.dispose(); 110 | } 111 | }; 112 | } -------------------------------------------------------------------------------- /win10/edge/test/error.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(error) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty error handle.") 26 | { 27 | jsrt::error handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | TEST_NO_CONTEXT_CALL(jsrt::error::create(L"foo")); 36 | runtime.dispose(); 37 | } 38 | 39 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 40 | { 41 | jsrt::runtime runtime = jsrt::runtime::create(); 42 | jsrt::context context = runtime.create_context(); 43 | { 44 | jsrt::context::scope scope(context); 45 | jsrt::error error; 46 | TEST_INVALID_ARG_CALL(error.message()); 47 | } 48 | runtime.dispose(); 49 | } 50 | 51 | MY_TEST_METHOD(create, "Test ::create method.") 52 | { 53 | jsrt::runtime runtime = jsrt::runtime::create(); 54 | jsrt::context context = runtime.create_context(); 55 | { 56 | jsrt::context::scope scope(context); 57 | jsrt::value value = jsrt::error::create(L"foo"); 58 | static_cast(value); 59 | } 60 | runtime.dispose(); 61 | } 62 | 63 | MY_TEST_METHOD(message, "Test messages.") 64 | { 65 | jsrt::runtime runtime = jsrt::runtime::create(); 66 | jsrt::context context = runtime.create_context(); 67 | { 68 | jsrt::context::scope scope(context); 69 | jsrt::value value = jsrt::error::create(L"foo"); 70 | jsrt::error error = static_cast(value); 71 | Assert::AreEqual(error.name(), static_cast(L"Error")); 72 | Assert::AreEqual(error.message(), static_cast(L"foo")); 73 | 74 | error = jsrt::error::create(L"%s %d", L"foo", 20); 75 | Assert::AreEqual(error.message(), static_cast(L"foo 20")); 76 | } 77 | runtime.dispose(); 78 | } 79 | 80 | MY_TEST_METHOD(specialized, "Test specialize errors.") 81 | { 82 | jsrt::runtime runtime = jsrt::runtime::create(); 83 | jsrt::context context = runtime.create_context(); 84 | { 85 | jsrt::context::scope scope(context); 86 | jsrt::error error; 87 | 88 | error = jsrt::error::create_range_error(L""); 89 | Assert::AreEqual(error.name(), static_cast(L"RangeError")); 90 | 91 | error = jsrt::error::create_reference_error(L""); 92 | Assert::AreEqual(error.name(), static_cast(L"ReferenceError")); 93 | 94 | error = jsrt::error::create_syntax_error(L""); 95 | Assert::AreEqual(error.name(), static_cast(L"SyntaxError")); 96 | 97 | error = jsrt::error::create_type_error(L""); 98 | Assert::AreEqual(error.name(), static_cast(L"TypeError")); 99 | 100 | error = jsrt::error::create_uri_error(L""); 101 | Assert::AreEqual(error.name(), static_cast(L"URIError")); 102 | } 103 | runtime.dispose(); 104 | } 105 | }; 106 | } -------------------------------------------------------------------------------- /win10/edge/test/jsrt-wrappers-test.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB} 23 | Win32Proj 24 | jsrtwrapperstest 25 | 10.0.10586.0 26 | 27 | 28 | 29 | DynamicLibrary 30 | true 31 | v140 32 | Unicode 33 | false 34 | 35 | 36 | DynamicLibrary 37 | false 38 | v140 39 | true 40 | Unicode 41 | false 42 | 43 | 44 | 45 | true 46 | 47 | 48 | 49 | Use 50 | Level3 51 | Disabled 52 | $(VCInstallDir)UnitTest\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) 53 | USE_EDGEMODE_JSRT;WIN32;_DEBUG;%(PreprocessorDefinitions) 54 | true 55 | true 56 | 57 | 58 | Windows 59 | true 60 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 61 | kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;chakrart.lib;%(AdditionalDependencies) 62 | 63 | 64 | true 65 | 66 | 67 | 68 | 69 | Level3 70 | Use 71 | MaxSpeed 72 | true 73 | true 74 | $(VCInstallDir)UnitTest\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) 75 | USE_EDGEMODE_JSRT;WIN32;NDEBUG;%(PreprocessorDefinitions) 76 | true 77 | 78 | 79 | Windows 80 | true 81 | true 82 | true 83 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 84 | chakrart.lib;%(AdditionalDependencies) 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | Create 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | {18b6a46c-9534-498d-8463-9822bc5cac42} 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /win10/edge/test/jsrt-wrappers-test.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | Source Files 64 | 65 | 66 | Source Files 67 | 68 | 69 | Source Files 70 | 71 | 72 | Source Files 73 | 74 | 75 | Source Files 76 | 77 | 78 | Source Files 79 | 80 | 81 | Source Files 82 | 83 | 84 | Source Files 85 | 86 | 87 | Source Files 88 | 89 | 90 | -------------------------------------------------------------------------------- /win10/edge/test/optional.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(optional) 24 | { 25 | public: 26 | MY_TEST_METHOD(optional_values, "Test optional.") 27 | { 28 | jsrt::optional o1; 29 | Assert::IsFalse(o1.has_value()); 30 | Assert::AreEqual(o1.value(), 0); 31 | o1.clear(); 32 | o1 = 10; 33 | Assert::IsTrue(o1.has_value()); 34 | Assert::AreEqual(o1.value(), 10); 35 | o1.clear(); 36 | Assert::IsFalse(o1.has_value()); 37 | Assert::AreEqual(o1.value(), 0); 38 | o1 = jsrt::missing(); 39 | Assert::IsFalse(o1.has_value()); 40 | Assert::AreEqual(o1.value(), 0); 41 | } 42 | }; 43 | } -------------------------------------------------------------------------------- /win10/edge/test/pinned.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(pinned) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_handle, "Test a pinned empty handle.") 27 | { 28 | jsrt::pinned handle; 29 | Assert::AreEqual(handle->handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(handle->is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(context_handle, "Test a pinned context handle.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::pinned context = static_cast>(runtime.create_context()); 37 | Assert::IsTrue(context->is_valid()); 38 | context.release(); 39 | Assert::IsFalse(context->is_valid()); 40 | runtime.dispose(); 41 | } 42 | 43 | MY_TEST_METHOD(handle_refcounting, "Test refcounting on a pinned handle.") 44 | { 45 | jsrt::runtime runtime = jsrt::runtime::create(); 46 | jsrt::context context = runtime.create_context(); 47 | { 48 | jsrt::pinned pinned_context = static_cast>(context); 49 | Assert::AreEqual(context.add_reference(), 2u); 50 | jsrt::pinned another_pinned_context = pinned_context; 51 | jsrt::pinned yet_another_pinned_context = std::move(pinned_context); 52 | Assert::IsTrue(yet_another_pinned_context->is_valid()); 53 | Assert::AreEqual(context.release(), 2u); 54 | } 55 | Assert::AreEqual(context.add_reference(), 1u); 56 | context.release(); 57 | runtime.dispose(); 58 | } 59 | }; 60 | } -------------------------------------------------------------------------------- /win10/edge/test/profiler.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | class Profiler sealed : public IActiveScriptProfilerCallback2 18 | { 19 | private: 20 | long m_refCount; 21 | 22 | public: 23 | Profiler(void) 24 | { 25 | m_refCount = 1; 26 | } 27 | 28 | ~Profiler(void) 29 | { 30 | } 31 | 32 | // IUnknown 33 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObj) override 34 | { 35 | if (riid == IID_IUnknown) 36 | { 37 | *ppvObj = static_cast(this); 38 | } 39 | else if (riid == IID_IActiveScriptProfilerCallback) 40 | { 41 | *ppvObj = static_cast(this); 42 | } 43 | else if (riid == IID_IActiveScriptProfilerCallback2) 44 | { 45 | *ppvObj = static_cast(this); 46 | } 47 | else 48 | { 49 | *ppvObj = nullptr; 50 | return E_NOINTERFACE; 51 | } 52 | 53 | 54 | AddRef(); 55 | return NOERROR; 56 | } 57 | 58 | ULONG STDMETHODCALLTYPE AddRef(void) override 59 | { 60 | return InterlockedIncrement(&m_refCount); 61 | } 62 | 63 | ULONG STDMETHODCALLTYPE Release(void) override 64 | { 65 | long lw; 66 | 67 | 68 | if (0 == (lw = InterlockedDecrement(&m_refCount))) 69 | { 70 | delete this; 71 | } 72 | return lw; 73 | } 74 | 75 | // IActiveScriptProfilerCallback 76 | HRESULT STDMETHODCALLTYPE Initialize(DWORD dwContext) override { return S_OK; } 77 | HRESULT STDMETHODCALLTYPE Shutdown(HRESULT hrReason) override { return S_OK; } 78 | HRESULT STDMETHODCALLTYPE ScriptCompiled(PROFILER_TOKEN scriptId, PROFILER_SCRIPT_TYPE type, IUnknown *pIDebugDocumentContext) override { return S_OK; } 79 | HRESULT STDMETHODCALLTYPE FunctionCompiled(PROFILER_TOKEN functionId, PROFILER_TOKEN scriptId, const wchar_t *pwszFunctionName, const wchar_t *pwszFunctionNameHint, IUnknown *pIDebugDocumentContext) override { return S_OK; } 80 | HRESULT STDMETHODCALLTYPE OnFunctionEnter(PROFILER_TOKEN scriptId, PROFILER_TOKEN functionId) override { return S_OK; } 81 | HRESULT STDMETHODCALLTYPE OnFunctionExit(PROFILER_TOKEN scriptId, PROFILER_TOKEN functionId) override { return S_OK; } 82 | 83 | // IActiveScriptProfilerCallback2 84 | HRESULT STDMETHODCALLTYPE OnFunctionEnterByName(const wchar_t *pwszFunctionName, PROFILER_SCRIPT_TYPE type) override { return S_OK; } 85 | HRESULT STDMETHODCALLTYPE OnFunctionExitByName(const wchar_t *pwszFunctionName, PROFILER_SCRIPT_TYPE type) override { return S_OK; } 86 | }; 87 | -------------------------------------------------------------------------------- /win10/edge/test/property_descriptor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(property_descriptor) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty property_descriptor handle.") 26 | { 27 | jsrt::property_descriptor handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::property_descriptor property_descriptor; 36 | TEST_NO_CONTEXT_CALL(jsrt::property_descriptor::create()); 37 | TEST_NO_CONTEXT_CALL(jsrt::property_descriptor::create(jsrt::function(), jsrt::function())); 38 | TEST_NO_CONTEXT_CALL(property_descriptor.writable()); 39 | TEST_NO_CONTEXT_CALL(property_descriptor.set_writable(true)); 40 | TEST_NO_CONTEXT_CALL(property_descriptor.enumerable()); 41 | TEST_NO_CONTEXT_CALL(property_descriptor.set_enumerable(true)); 42 | TEST_NO_CONTEXT_CALL(property_descriptor.configurable()); 43 | TEST_NO_CONTEXT_CALL(property_descriptor.set_configurable(true)); 44 | TEST_NO_CONTEXT_CALL(property_descriptor.value()); 45 | TEST_NO_CONTEXT_CALL(property_descriptor.set_value(true)); 46 | TEST_NO_CONTEXT_CALL(property_descriptor.getter()); 47 | TEST_NO_CONTEXT_CALL(property_descriptor.set_getter(jsrt::function())); 48 | TEST_NO_CONTEXT_CALL(property_descriptor.setter()); 49 | TEST_NO_CONTEXT_CALL(property_descriptor.set_setter(jsrt::function())); 50 | runtime.dispose(); 51 | } 52 | 53 | static bool b(const jsrt::call_info &info) 54 | { 55 | return true; 56 | } 57 | 58 | static void set_b(const jsrt::call_info &info, bool v) 59 | { 60 | Assert::IsTrue(v); 61 | } 62 | 63 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 64 | { 65 | jsrt::runtime runtime = jsrt::runtime::create(); 66 | jsrt::context context = runtime.create_context(); 67 | { 68 | jsrt::context::scope scope(context); 69 | jsrt::property_descriptor property_descriptor; 70 | TEST_INVALID_ARG_CALL(property_descriptor.writable()); 71 | TEST_INVALID_ARG_CALL(property_descriptor.set_writable(true)); 72 | TEST_INVALID_ARG_CALL(property_descriptor.enumerable()); 73 | TEST_INVALID_ARG_CALL(property_descriptor.set_enumerable(true)); 74 | TEST_INVALID_ARG_CALL(property_descriptor.configurable()); 75 | TEST_INVALID_ARG_CALL(property_descriptor.set_configurable(true)); 76 | TEST_INVALID_ARG_CALL(property_descriptor.value()); 77 | TEST_INVALID_ARG_CALL(property_descriptor.set_value(true)); 78 | TEST_INVALID_ARG_CALL(property_descriptor.getter()); 79 | TEST_INVALID_ARG_CALL(property_descriptor.set_getter(jsrt::function())); 80 | TEST_INVALID_ARG_CALL(property_descriptor.setter()); 81 | TEST_INVALID_ARG_CALL(property_descriptor.set_setter(jsrt::function())); 82 | 83 | jsrt::property_descriptor::create(); 84 | TEST_INVALID_ARG_CALL(jsrt::property_descriptor::create(jsrt::function(), jsrt::function())); 85 | TEST_INVALID_ARG_CALL(jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function())); 86 | TEST_INVALID_ARG_CALL(property_descriptor.set_getter(jsrt::function())); 87 | TEST_INVALID_ARG_CALL(property_descriptor.set_setter(jsrt::function())); 88 | } 89 | runtime.dispose(); 90 | } 91 | 92 | MY_TEST_METHOD(create, "Test ::create method.") 93 | { 94 | jsrt::runtime runtime = jsrt::runtime::create(); 95 | jsrt::context context = runtime.create_context(); 96 | { 97 | jsrt::context::scope scope(context); 98 | jsrt::value value = jsrt::property_descriptor::create(); 99 | jsrt::property_descriptor property_descriptor = static_cast>(value); 100 | Assert::IsTrue(property_descriptor.is_valid()); 101 | property_descriptor = jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function::create(set_b)); 102 | Assert::IsTrue(property_descriptor.is_valid()); 103 | } 104 | runtime.dispose(); 105 | } 106 | 107 | MY_TEST_METHOD(descriptors, "Test descriptor methods.") 108 | { 109 | jsrt::runtime runtime = jsrt::runtime::create(); 110 | jsrt::context context = runtime.create_context(); 111 | { 112 | jsrt::context::scope scope(context); 113 | jsrt::object object = jsrt::object::create(); 114 | 115 | jsrt::property_descriptor desc = jsrt::property_descriptor::create(); 116 | desc.set_configurable(false); 117 | desc.set_enumerable(false); 118 | desc.set_writable(false); 119 | desc.set_value(10); 120 | object.define_property(jsrt::property_id::create(L"a"), desc); 121 | Assert::AreEqual(object.get_property(jsrt::property_id::create(L"a")), 10.0); 122 | desc = object.get_own_property_descriptor(jsrt::property_id::create(L"a")); 123 | Assert::IsFalse(desc.configurable()); 124 | Assert::IsFalse(desc.enumerable()); 125 | Assert::IsFalse(desc.writable()); 126 | Assert::AreEqual(desc.value(), 10.0); 127 | jsrt::property_descriptor bdesc = jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function::create(set_b)); 128 | object.define_property(jsrt::property_id::create(L"b"), bdesc); 129 | Assert::IsTrue(object.get_property(jsrt::property_id::create(L"b"))); 130 | object.set_property(jsrt::property_id::create(L"b"), true); 131 | } 132 | runtime.dispose(); 133 | } 134 | }; 135 | } -------------------------------------------------------------------------------- /win10/edge/test/property_id.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(property_id) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_id, "Test an empty property ID.") 27 | { 28 | jsrt::property_id id; 29 | Assert::AreEqual(id.handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(id.is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(name, "Test a string property ID.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::context context = runtime.create_context(); 37 | { 38 | jsrt::context::scope scope(context); 39 | jsrt::property_id id = jsrt::property_id::create(L"foo"); 40 | Assert::AreEqual(id.name(), static_cast(L"foo")); 41 | TEST_FAILED_CALL(id.symbol(), property_not_symbol_exception); 42 | } 43 | runtime.dispose(); 44 | } 45 | 46 | MY_TEST_METHOD(symbol, "Test a symbol property ID.") 47 | { 48 | jsrt::runtime runtime = jsrt::runtime::create(); 49 | jsrt::context context = runtime.create_context(); 50 | { 51 | jsrt::context::scope scope(context); 52 | jsrt::symbol symbol = jsrt::symbol::create(L"foo"); 53 | jsrt::property_id id = jsrt::property_id::create(symbol); 54 | Assert::IsTrue(id.symbol().strict_equals(symbol)); 55 | TEST_FAILED_CALL(id.name(), property_not_string_exception); 56 | } 57 | runtime.dispose(); 58 | } 59 | 60 | MY_TEST_METHOD(type, "Test property ID type.") 61 | { 62 | jsrt::runtime runtime = jsrt::runtime::create(); 63 | jsrt::context context = runtime.create_context(); 64 | { 65 | jsrt::context::scope scope(context); 66 | jsrt::symbol symbol = jsrt::symbol::create(L"foo"); 67 | jsrt::property_id id = jsrt::property_id::create(symbol); 68 | Assert::AreEqual(id.type(), JsPropertyIdTypeSymbol); 69 | 70 | id = jsrt::property_id::create(L"foo"); 71 | Assert::AreEqual(id.type(), JsPropertyIdTypeString); 72 | } 73 | runtime.dispose(); 74 | } 75 | 76 | MY_TEST_METHOD(invalid, "Test property ID methods with invalid ID.") 77 | { 78 | jsrt::runtime runtime = jsrt::runtime::create(); 79 | jsrt::context context = runtime.create_context(); 80 | { 81 | jsrt::context::scope scope(context); 82 | try 83 | { 84 | jsrt::property_id id; 85 | id.name(); 86 | Assert::Fail(); 87 | } 88 | catch (jsrt::invalid_argument_exception) 89 | { 90 | } 91 | } 92 | runtime.dispose(); 93 | } 94 | 95 | MY_TEST_METHOD(no_context, "Test property ID methods with no context.") 96 | { 97 | jsrt::runtime runtime = jsrt::runtime::create(); 98 | jsrt::context context = runtime.create_context(); 99 | TEST_NO_CONTEXT_CALL(jsrt::property_id::create(L"foo")); 100 | jsrt::property_id foo; 101 | { 102 | jsrt::context::scope scope(context); 103 | foo = jsrt::property_id::create(L"foo"); 104 | } 105 | foo.name(); 106 | runtime.dispose(); 107 | } 108 | }; 109 | } -------------------------------------------------------------------------------- /win10/edge/test/readme.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(readme) 23 | { 24 | public: 25 | static double add(const jsrt::call_info &info, double a, double b) 26 | { 27 | return a + b; 28 | } 29 | 30 | MY_TEST_METHOD(samples, "Test readme samples.") 31 | { 32 | jsrt::runtime runtime = jsrt::runtime::create(); 33 | jsrt::context context = runtime.create_context(); 34 | { 35 | jsrt::context::scope scope(context); 36 | jsrt::pinned pinned_obj = static_cast>(jsrt::object::create()); 37 | 38 | jsrt::object obj = jsrt::object::create(); 39 | obj.set_property(jsrt::property_id::create(L"boolProperty"), true); 40 | obj.get_property(jsrt::property_id::create(L"boolProperty")); 41 | obj.set_property(jsrt::property_id::create(L"stringProperty"), L"foo"); 42 | 43 | jsrt::array darray = jsrt::array::create(1); 44 | darray[0] = 10; 45 | darray[1] = 20; 46 | 47 | auto f = static_cast>(jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;")); 48 | f(jsrt::context::undefined(), 1, 2); 49 | 50 | auto nf = jsrt::function::create(add); 51 | jsrt::context::global().set_property(jsrt::property_id::create(L"add"), nf); 52 | jsrt::context::run(L"add(1, 2)"); 53 | 54 | auto bf = jsrt::bound_function( 55 | jsrt::context::undefined(), 56 | static_cast>(jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;"))); 57 | bf(1, 2); 58 | } 59 | runtime.dispose(); 60 | } 61 | }; 62 | } -------------------------------------------------------------------------------- /win10/edge/test/reference.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(reference) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty handle.") 26 | { 27 | jsrt::reference handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(context_handle, "Test a context handle.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | Assert::IsTrue(context.is_valid()); 37 | runtime.dispose(); 38 | } 39 | 40 | MY_TEST_METHOD(handle_refcounting, "Test refcounting on a handle.") 41 | { 42 | jsrt::runtime runtime = jsrt::runtime::create(); 43 | jsrt::context context = runtime.create_context(); 44 | Assert::AreEqual(context.add_reference(), static_cast(1)); 45 | Assert::AreEqual(context.release(), static_cast(0)); 46 | runtime.dispose(); 47 | } 48 | 49 | MY_TEST_METHOD(invalid_handle, "Test refcounting on an invalid handle.") 50 | { 51 | jsrt::context context; 52 | 53 | TEST_INVALID_ARG_CALL(context.add_reference()); 54 | TEST_INVALID_ARG_CALL(context.release()); 55 | TEST_INVALID_ARG_CALL(context.set_before_collect_callback(nullptr, nullptr)) 56 | } 57 | 58 | static int callback_count; 59 | 60 | static void CALLBACK collect_callback(JsRef ref, void *callbackState) 61 | { 62 | Assert::AreEqual(callbackState, reinterpret_cast(0xdeadbeef)); 63 | TEST_FAILED_CALL(jsrt::error::create(L"test"), in_object_before_collect_callback_exception); 64 | callback_count++; 65 | } 66 | 67 | MY_TEST_METHOD(collection_and_callbacks, "Test ::set_before_collect_callback.") 68 | { 69 | jsrt::runtime runtime = jsrt::runtime::create(); 70 | callback_count = 0; 71 | { 72 | jsrt::context context = runtime.create_context(); 73 | jsrt::context::scope scope(context); 74 | jsrt::object object = jsrt::object::create(); 75 | object.set_before_collect_callback(reinterpret_cast(0xdeadbeef), collect_callback); 76 | object = jsrt::object(); 77 | runtime.collect_garbage(); 78 | } 79 | Assert::AreNotEqual(callback_count, 0); 80 | runtime.dispose(); 81 | } 82 | }; 83 | 84 | int reference::callback_count = 0; 85 | } -------------------------------------------------------------------------------- /win10/edge/test/runtime.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(runtime) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty handle.") 26 | { 27 | jsrt::runtime runtime; 28 | Assert::AreEqual(runtime.handle(), JS_INVALID_RUNTIME_HANDLE); 29 | Assert::IsFalse(runtime.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(create_dispose, "Test the ::create and ::dispose methods.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | Assert::AreNotEqual(runtime.handle(), JS_INVALID_RUNTIME_HANDLE); 36 | Assert::IsTrue(runtime.is_valid()); 37 | runtime.dispose(); 38 | } 39 | 40 | MY_TEST_METHOD(invalid_handle, "Test APIs on an invalid handle.") 41 | { 42 | jsrt::runtime runtime; 43 | TEST_INVALID_ARG_CALL(runtime.dispose()) 44 | TEST_INVALID_ARG_CALL(runtime.memory_usage()) 45 | TEST_INVALID_ARG_CALL(runtime.memory_limit()) 46 | TEST_INVALID_ARG_CALL(runtime.set_memory_limit(-1)) 47 | TEST_INVALID_ARG_CALL(runtime.set_memory_allocation_callback(nullptr, nullptr)) 48 | TEST_INVALID_ARG_CALL(runtime.set_before_collect_callback(nullptr, nullptr)) 49 | TEST_INVALID_ARG_CALL(runtime.collect_garbage()) 50 | TEST_INVALID_ARG_CALL(runtime.disable_execution()) 51 | TEST_INVALID_ARG_CALL(runtime.enable_execution()) 52 | TEST_INVALID_ARG_CALL(runtime.is_execution_disabled()) 53 | TEST_INVALID_ARG_CALL(runtime.create_context()) 54 | } 55 | 56 | MY_TEST_METHOD(memory_usage, "Test ::memory_usage method.") 57 | { 58 | jsrt::runtime runtime = jsrt::runtime::create(); 59 | size_t usage = runtime.memory_usage(); 60 | Assert::AreNotEqual(usage, static_cast(0)); 61 | runtime.dispose(); 62 | } 63 | 64 | MY_TEST_METHOD(memory_limit, "Test ::memory_limit and ::set_memory_limit methods.") 65 | { 66 | const size_t onegb = 1024 * 1024 * 1024; 67 | 68 | jsrt::runtime runtime = jsrt::runtime::create(); 69 | size_t limit = runtime.memory_limit(); 70 | Assert::AreEqual(limit, static_cast(-1)); 71 | runtime.set_memory_limit(onegb); 72 | limit = runtime.memory_limit(); 73 | Assert::AreEqual(limit, onegb); 74 | runtime.set_memory_limit(static_cast(-1)); 75 | limit = runtime.memory_limit(); 76 | Assert::AreEqual(limit, static_cast(-1)); 77 | runtime.dispose(); 78 | } 79 | 80 | static int callback_count; 81 | 82 | static bool CALLBACK allocation_callback(void *callbackState, JsMemoryEventType allocationEvent, size_t allocationSize) 83 | { 84 | Assert::AreEqual(callbackState, reinterpret_cast(0xdeadbeef)); 85 | callback_count++; 86 | return true; 87 | } 88 | 89 | MY_TEST_METHOD(memory_allocation_callback, "Test ::set_memory_allocation_callback method.") 90 | { 91 | jsrt::runtime runtime = jsrt::runtime::create(); 92 | callback_count = 0; 93 | runtime.set_memory_allocation_callback(reinterpret_cast(0xdeadbeef), allocation_callback); 94 | { 95 | jsrt::context context = runtime.create_context(); 96 | jsrt::context::scope scope(context); 97 | } 98 | runtime.set_memory_allocation_callback(nullptr, nullptr); 99 | Assert::AreNotEqual(callback_count, 0); 100 | runtime.dispose(); 101 | } 102 | 103 | static void CALLBACK collect_callback(void *callbackState) 104 | { 105 | Assert::AreEqual(callbackState, reinterpret_cast(0xdeadbeef)); 106 | callback_count++; 107 | } 108 | 109 | MY_TEST_METHOD(collection_and_callbacks, "Test ::set_before_collect_callback and ::collect_garbage.") 110 | { 111 | jsrt::runtime runtime = jsrt::runtime::create(); 112 | callback_count = 0; 113 | runtime.set_before_collect_callback(reinterpret_cast(0xdeadbeef), collect_callback); 114 | { 115 | jsrt::context context = runtime.create_context(); 116 | jsrt::context::scope scope(context); 117 | runtime.collect_garbage(); 118 | } 119 | runtime.set_before_collect_callback(nullptr, nullptr); 120 | Assert::AreNotEqual(callback_count, 0); 121 | runtime.dispose(); 122 | } 123 | 124 | MY_TEST_METHOD(invalid_disable, "Test ::enable_execution and ::disable_execution in a runtime that doesn't allow it.") 125 | { 126 | jsrt::runtime runtime = jsrt::runtime::create(); 127 | try 128 | { 129 | runtime.disable_execution(); 130 | Assert::Fail(); 131 | } 132 | catch (const jsrt::cannot_disable_execution_exception &) 133 | { 134 | // Expected 135 | } 136 | 137 | runtime.enable_execution(); 138 | Assert::IsFalse(runtime.is_execution_disabled()); 139 | runtime.dispose(); 140 | } 141 | 142 | MY_TEST_METHOD(disable, "Test ::enable_execution and ::disable_execution methods.") 143 | { 144 | jsrt::runtime runtime = jsrt::runtime::create(JsRuntimeAttributeAllowScriptInterrupt); 145 | { 146 | jsrt::context context = runtime.create_context(); 147 | jsrt::context::scope scope(context); 148 | runtime.disable_execution(); 149 | Assert::IsTrue(runtime.is_execution_disabled()); 150 | try 151 | { 152 | context.has_exception(); 153 | Assert::Fail(); 154 | } 155 | catch (const jsrt::in_disabled_state_exception &) 156 | { 157 | // Expected 158 | } 159 | runtime.enable_execution(); 160 | Assert::IsFalse(runtime.is_execution_disabled()); 161 | Assert::IsFalse(context.has_exception()); 162 | } 163 | runtime.dispose(); 164 | } 165 | 166 | MY_TEST_METHOD(create_debug_context, "Test creating a context with debugging.") 167 | { 168 | jsrt::runtime runtime = jsrt::runtime::create(); 169 | { 170 | jsrt::context context = runtime.create_context(); 171 | jsrt::context::scope scope(context); 172 | } 173 | runtime.dispose(); 174 | } 175 | 176 | MY_TEST_METHOD(disable_eval, "Test disabling eval in a runtime.") 177 | { 178 | jsrt::runtime runtime = jsrt::runtime::create(JsRuntimeAttributeDisableEval); 179 | { 180 | jsrt::context context = runtime.create_context(); 181 | jsrt::context::scope scope(context); 182 | 183 | try 184 | { 185 | context.run(L"eval(\"1 + 2\");"); 186 | Assert::Fail(); 187 | } 188 | catch (const jsrt::script_eval_disabled_exception &) 189 | { 190 | // Expected 191 | } 192 | } 193 | runtime.dispose(); 194 | } 195 | 196 | MY_TEST_METHOD(runtime_settings, "Test runtime settings.") 197 | { 198 | // JsRuntimeAttributeAllowScriptInterrupt is tested above in invalid_disable/disable 199 | // JsRuntimeAttributeEnableIdleProcessing is tested with contexts 200 | // JsRuntimeAttributeDisableEval is tested in disable_eval 201 | jsrt::runtime runtime = jsrt::runtime::create( 202 | static_cast(JsRuntimeAttributeDisableBackgroundWork | JsRuntimeAttributeDisableNativeCodeGeneration)); 203 | runtime.dispose(); 204 | 205 | runtime = jsrt::runtime::create(JsRuntimeAttributeNone); 206 | runtime.dispose(); 207 | 208 | TEST_INVALID_ARG_CALL(jsrt::runtime::create(static_cast(0xFFFFFFFF))); 209 | } 210 | 211 | static bool CALLBACK service_callback(JsBackgroundWorkItemCallback callback, void *callbackState) 212 | { 213 | callback(callbackState); 214 | callback_count++; 215 | return true; 216 | } 217 | 218 | MY_TEST_METHOD(background_callback, "Test background work item callbacks.") 219 | { 220 | jsrt::runtime runtime = jsrt::runtime::create(JsRuntimeAttributeNone, service_callback); 221 | callback_count = 0; 222 | { 223 | jsrt::context context = runtime.create_context(); 224 | jsrt::context::scope scope(context); 225 | runtime.collect_garbage(); 226 | } 227 | Assert::AreNotEqual(callback_count, 0); 228 | runtime.dispose(); 229 | } 230 | }; 231 | 232 | int runtime::callback_count = 0; 233 | } -------------------------------------------------------------------------------- /win10/edge/test/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | -------------------------------------------------------------------------------- /win10/edge/test/stdafx.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "targetver.h" 18 | #define WIN32_LEAN_AND_MEAN 19 | #include "jsrt.h" 20 | #include "jsrt-wrappers.h" 21 | #include "profiler.h" 22 | #include "CppUnitTest.h" 23 | 24 | #include 25 | #include 26 | 27 | #define MY_TEST_METHOD(name, description) \ 28 | BEGIN_TEST_METHOD_ATTRIBUTE(name) \ 29 | TEST_DESCRIPTION(description) \ 30 | END_TEST_METHOD_ATTRIBUTE() \ 31 | \ 32 | TEST_METHOD(name) 33 | 34 | #define MY_TEST_METHOD_DISABLED(name, description) \ 35 | BEGIN_TEST_METHOD_ATTRIBUTE(name) \ 36 | TEST_DESCRIPTION(description) \ 37 | TEST_IGNORE() \ 38 | END_TEST_METHOD_ATTRIBUTE() \ 39 | \ 40 | TEST_METHOD(name) 41 | 42 | #define TEST_FAILED_CALL(call, exception) \ 43 | try \ 44 | { \ 45 | call; \ 46 | Assert::Fail(); \ 47 | } \ 48 | catch (const jsrt:: exception &) \ 49 | { \ 50 | } 51 | 52 | #define TEST_INVALID_ARG_CALL(call) \ 53 | TEST_FAILED_CALL(call, invalid_argument_exception) 54 | 55 | #define TEST_NO_CONTEXT_CALL(call) \ 56 | TEST_FAILED_CALL(call, no_current_context_exception) 57 | 58 | #define TEST_NULL_ARG_CALL(call) \ 59 | TEST_FAILED_CALL(call, null_argument_exception) 60 | 61 | #define TEST_SCRIPT_EXCEPTION_CALL(call) \ 62 | TEST_FAILED_CALL(call, script_exception) 63 | 64 | template <> 65 | static std::wstring Microsoft::VisualStudio::CppUnitTestFramework::ToString(const JsValueType& q) 66 | { 67 | switch (q) 68 | { 69 | case JsUndefined: 70 | return L"JsUndefined"; 71 | case JsNull: 72 | return L"JsNull"; 73 | case JsNumber: 74 | return L"JsNumber"; 75 | case JsString: 76 | return L"JsString"; 77 | case JsBoolean: 78 | return L"JsBoolean"; 79 | case JsObject: 80 | return L"JsObject"; 81 | case JsFunction: 82 | return L"JsFunction"; 83 | case JsError: 84 | return L"JsError"; 85 | case JsArray: 86 | return L"JsArray"; 87 | case JsSymbol: 88 | return L"JsSymbol"; 89 | case JsArrayBuffer: 90 | return L"JsArrayBuffer"; 91 | case JsTypedArray: 92 | return L"JsTypedArray"; 93 | case JsDataView: 94 | return L"JsDataView"; 95 | default: 96 | return std::wstring(); 97 | } 98 | } 99 | 100 | template <> 101 | static std::wstring Microsoft::VisualStudio::CppUnitTestFramework::ToString(const JsPropertyIdType& q) 102 | { 103 | switch (q) 104 | { 105 | case JsPropertyIdTypeString: 106 | return L"JsPropertyIdTypeString"; 107 | case JsPropertyIdTypeSymbol: 108 | return L"JsPropertyIdTypeSymbol"; 109 | default: 110 | return std::wstring(); 111 | } 112 | } 113 | 114 | template<> 115 | static std::wstring Microsoft::VisualStudio::CppUnitTestFramework::ToString(const JsTypedArrayType& q) 116 | { 117 | switch (q) 118 | { 119 | case JsArrayTypeInt8: 120 | return L"JsArrayTypeInt8"; 121 | case JsArrayTypeUint8: 122 | return L"JsArrayTypeUint8"; 123 | case JsArrayTypeUint8Clamped: 124 | return L"JsArrayTypeUint8Clamped"; 125 | case JsArrayTypeInt16: 126 | return L"JsArrayTypeInt16"; 127 | case JsArrayTypeUint16: 128 | return L"JsArrayTypeUint16"; 129 | case JsArrayTypeInt32: 130 | return L"JsArrayTypeInt32"; 131 | case JsArrayTypeUint32: 132 | return L"JsArrayTypeUint32"; 133 | case JsArrayTypeFloat32: 134 | return L"JsArrayTypeFloat32"; 135 | case JsArrayTypeFloat64: 136 | return L"JsArrayTypeFloat64"; 137 | default: 138 | return std::wstring(); 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /win10/edge/test/symbol.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(symbol) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty symbol handle.") 26 | { 27 | jsrt::symbol handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | TEST_NO_CONTEXT_CALL(jsrt::symbol::create(L"foo")); 36 | runtime.dispose(); 37 | } 38 | 39 | MY_TEST_METHOD(create, "Test creating a symbol.") 40 | { 41 | jsrt::runtime runtime = jsrt::runtime::create(); 42 | jsrt::context context = runtime.create_context(); 43 | { 44 | jsrt::context::scope scope(context); 45 | jsrt::symbol::create(L"foo"); 46 | jsrt::symbol::create(std::wstring()); 47 | } 48 | runtime.dispose(); 49 | } 50 | }; 51 | } -------------------------------------------------------------------------------- /win10/edge/test/targetver.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | -------------------------------------------------------------------------------- /win10/edge/test/typed_array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(typed_array) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty typed_array handle.") 26 | { 27 | jsrt::typed_array handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::typed_array array; 37 | TEST_NO_CONTEXT_CALL(jsrt::typed_array::create(0)); 38 | { 39 | jsrt::context::scope scope(context); 40 | array = jsrt::typed_array::create(0); 41 | } 42 | array.data(); 43 | array.data_size(); 44 | array.element_size(); 45 | array.type(); 46 | runtime.dispose(); 47 | } 48 | 49 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 50 | { 51 | jsrt::runtime runtime = jsrt::runtime::create(); 52 | jsrt::context context = runtime.create_context(); 53 | { 54 | jsrt::context::scope scope(context); 55 | jsrt::typed_array array; 56 | TEST_INVALID_ARG_CALL(array.data_size()); 57 | TEST_INVALID_ARG_CALL(array.element_size()); 58 | TEST_INVALID_ARG_CALL(array.data()); 59 | TEST_INVALID_ARG_CALL(array.type()); 60 | } 61 | runtime.dispose(); 62 | } 63 | 64 | MY_TEST_METHOD(create, "Test ::create method.") 65 | { 66 | jsrt::runtime runtime = jsrt::runtime::create(); 67 | jsrt::context context = runtime.create_context(); 68 | { 69 | jsrt::context::scope scope(context); 70 | jsrt::value value = jsrt::typed_array::create(5); 71 | static_cast>(value); 72 | 73 | jsrt::array_buffer buffer = jsrt::array_buffer::create(8); 74 | jsrt::typed_array array = jsrt::typed_array::create(buffer, 2, 3); 75 | 76 | jsrt::typed_array::create(array); 77 | 78 | jsrt::array base_array = jsrt::array::create(5); 79 | jsrt::typed_array::create(base_array); 80 | 81 | jsrt::typed_array::create({ 1, 2, 3, 4 }); 82 | } 83 | runtime.dispose(); 84 | } 85 | 86 | MY_TEST_METHOD(types, "Test types.") 87 | { 88 | jsrt::runtime runtime = jsrt::runtime::create(); 89 | jsrt::context context = runtime.create_context(); 90 | { 91 | jsrt::context::scope scope(context); 92 | 93 | jsrt::typed_array::create(1); 94 | jsrt::typed_array::create(1); 95 | jsrt::typed_array::create(1); 96 | jsrt::typed_array::create(1); 97 | jsrt::typed_array::create(1); 98 | jsrt::typed_array::create(1); 99 | jsrt::typed_array::create(1); 100 | jsrt::typed_array::create(1); 101 | jsrt::typed_array::create(1); 102 | } 103 | runtime.dispose(); 104 | } 105 | 106 | MY_TEST_METHOD(data, "Test data and size.") 107 | { 108 | jsrt::runtime runtime = jsrt::runtime::create(); 109 | jsrt::context context = runtime.create_context(); 110 | { 111 | jsrt::context::scope scope(context); 112 | jsrt::array_buffer buffer = jsrt::array_buffer::create(8); 113 | jsrt::typed_array array = jsrt::typed_array::create(buffer, 2, 3); 114 | Assert::AreEqual(array.type(), JsArrayTypeInt16); 115 | Assert::AreEqual(array.data_size(), 6u); 116 | Assert::AreEqual(array.element_size(), 2); 117 | Assert::IsNotNull(array.data()); 118 | } 119 | runtime.dispose(); 120 | } 121 | 122 | MY_TEST_METHOD(indexing, "Test indexing.") 123 | { 124 | jsrt::runtime runtime = jsrt::runtime::create(); 125 | jsrt::context context = runtime.create_context(); 126 | { 127 | jsrt::context::scope scope(context); 128 | jsrt::array_buffer buffer = jsrt::array_buffer::create(64); 129 | jsrt::typed_array darray = jsrt::typed_array::create(buffer, 0, 2); 130 | darray[0] = 10; 131 | darray[1] = 20; 132 | Assert::AreEqual(static_cast(darray[0]), 10.0); 133 | Assert::AreEqual(static_cast(darray[1]), 20.0); 134 | 135 | jsrt::typed_array iarray = jsrt::typed_array::create(buffer, 0, 2); 136 | iarray[0] = 30; 137 | iarray[1] = 40; 138 | Assert::AreEqual(static_cast(iarray[0]), 30); 139 | Assert::AreEqual(static_cast(iarray[1]), 40); 140 | } 141 | runtime.dispose(); 142 | } 143 | }; 144 | } -------------------------------------------------------------------------------- /win10/edge/test/value.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(value) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty value handle.") 26 | { 27 | jsrt::value handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | 31 | jsrt::boolean boolean_handle; 32 | Assert::AreEqual(boolean_handle.handle(), JS_INVALID_REFERENCE); 33 | Assert::IsFalse(boolean_handle.is_valid()); 34 | 35 | jsrt::number number_handle; 36 | Assert::AreEqual(number_handle.handle(), JS_INVALID_REFERENCE); 37 | Assert::IsFalse(number_handle.is_valid()); 38 | 39 | jsrt::string string_handle; 40 | Assert::AreEqual(string_handle.handle(), JS_INVALID_REFERENCE); 41 | Assert::IsFalse(string_handle.is_valid()); 42 | } 43 | 44 | MY_TEST_METHOD(no_context, "Test calls with no context.") 45 | { 46 | jsrt::runtime runtime = jsrt::runtime::create(); 47 | jsrt::context context = runtime.create_context(); 48 | jsrt::value value; 49 | VARIANT v; 50 | { 51 | jsrt::context::scope scope(context); 52 | value = jsrt::number::create(10); 53 | } 54 | value.type(); 55 | TEST_NO_CONTEXT_CALL(value.to_variant(&v)); 56 | TEST_NO_CONTEXT_CALL(jsrt::value::from_variant(&v)); 57 | TEST_NO_CONTEXT_CALL(jsrt::boolean::create(true)); 58 | TEST_NO_CONTEXT_CALL(jsrt::boolean::true_value()); 59 | TEST_NO_CONTEXT_CALL(jsrt::boolean::false_value()); 60 | { 61 | jsrt::context::scope scope(context); 62 | value = jsrt::boolean::true_value(); 63 | } 64 | static_cast(value).data(); 65 | TEST_NO_CONTEXT_CALL(jsrt::number::create(1.0)); 66 | { 67 | jsrt::context::scope scope(context); 68 | value = jsrt::number::create(10.0); 69 | } 70 | static_cast(value).as_double(); 71 | { 72 | jsrt::context::scope scope(context); 73 | value = jsrt::number::create(10); 74 | } 75 | static_cast(value).as_int(); 76 | TEST_NO_CONTEXT_CALL(jsrt::string::create(L"foo")); 77 | { 78 | jsrt::context::scope scope(context); 79 | value = jsrt::string::create(L"foo"); 80 | } 81 | static_cast(value).data(); 82 | static_cast(value).length(); 83 | runtime.dispose(); 84 | } 85 | 86 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 87 | { 88 | jsrt::runtime runtime = jsrt::runtime::create(); 89 | jsrt::context context = runtime.create_context(); 90 | { 91 | jsrt::context::scope scope(context); 92 | jsrt::value value; 93 | VARIANT v; 94 | TEST_INVALID_ARG_CALL(value.type()); 95 | TEST_INVALID_ARG_CALL(value.to_variant(&v)); 96 | jsrt::boolean boolean; 97 | TEST_INVALID_ARG_CALL(boolean.data()); 98 | jsrt::number number; 99 | TEST_INVALID_ARG_CALL(number.as_int()); 100 | TEST_INVALID_ARG_CALL(number.as_double()); 101 | jsrt::string string; 102 | TEST_INVALID_ARG_CALL(string.data()); 103 | TEST_INVALID_ARG_CALL(string.length()); 104 | } 105 | runtime.dispose(); 106 | } 107 | 108 | MY_TEST_METHOD(type, "Test the ::type method.") 109 | { 110 | jsrt::runtime runtime = jsrt::runtime::create(); 111 | jsrt::context context = runtime.create_context(); 112 | { 113 | jsrt::context::scope scope(context); 114 | jsrt::value value; 115 | value = jsrt::context::undefined(); 116 | Assert::AreEqual(value.type(), JsUndefined); 117 | value = jsrt::context::null(); 118 | Assert::AreEqual(value.type(), JsNull); 119 | value = jsrt::boolean::true_value(); 120 | Assert::AreEqual(value.type(), JsBoolean); 121 | value = jsrt::number::create(10); 122 | Assert::AreEqual(value.type(), JsNumber); 123 | value = jsrt::string::create(L"foo"); 124 | Assert::AreEqual(value.type(), JsString); 125 | value = jsrt::object::create(); 126 | Assert::AreEqual(value.type(), JsObject); 127 | value = jsrt::context::parse(L"1 + 2;"); 128 | Assert::AreEqual(value.type(), JsFunction); 129 | value = jsrt::error::create_uri_error(L"foo"); 130 | Assert::AreEqual(value.type(), JsError); 131 | value = jsrt::array::create(10); 132 | Assert::AreEqual(value.type(), JsArray); 133 | value = jsrt::symbol::create(L"foo"); 134 | Assert::AreEqual(value.type(), JsSymbol); 135 | value = jsrt::array_buffer::create(0); 136 | Assert::AreEqual(value.type(), JsArrayBuffer); 137 | value = jsrt::data_view<>::create(jsrt::array_buffer::create(0), 0, 0); 138 | Assert::AreEqual(value.type(), JsDataView); 139 | value = jsrt::typed_array::create(5); 140 | Assert::AreEqual(value.type(), JsTypedArray); 141 | } 142 | runtime.dispose(); 143 | } 144 | 145 | MY_TEST_METHOD(variant, "Test the ::to_variant and ::from_variant methods.") 146 | { 147 | jsrt::runtime runtime = jsrt::runtime::create(); 148 | jsrt::context context = runtime.create_context(); 149 | { 150 | jsrt::context::scope scope(context); 151 | VARIANT v; 152 | v.vt = VT_I4; 153 | v.intVal = 42; 154 | jsrt::value value = jsrt::value::from_variant(&v); 155 | Assert::AreEqual(static_cast(value.type()), static_cast(JsNumber)); 156 | Assert::AreEqual(static_cast(value).as_int(), 42); 157 | value = jsrt::number::create(32); 158 | value.to_variant(&v); 159 | Assert::AreEqual(static_cast(v.vt), static_cast(VT_I4)); 160 | Assert::AreEqual(v.intVal, 32); 161 | } 162 | runtime.dispose(); 163 | } 164 | 165 | MY_TEST_METHOD(equals, "Test the ::equals and ::strict_equals methods.") 166 | { 167 | jsrt::runtime runtime = jsrt::runtime::create(); 168 | jsrt::context context = runtime.create_context(); 169 | { 170 | jsrt::context::scope scope(context); 171 | jsrt::value v1 = jsrt::number::create(1); 172 | jsrt::value v2 = jsrt::string::create(L"1"); 173 | jsrt::value v3 = jsrt::number::create(1); 174 | Assert::IsTrue(v1.equals(v2)); 175 | Assert::IsTrue(v1.equals(v3)); 176 | Assert::IsFalse(v1.strict_equals(v2)); 177 | Assert::IsTrue(v1.strict_equals(v3)); 178 | } 179 | runtime.dispose(); 180 | } 181 | 182 | MY_TEST_METHOD(boolean, "Test boolean methods.") 183 | { 184 | jsrt::runtime runtime = jsrt::runtime::create(); 185 | jsrt::context context = runtime.create_context(); 186 | { 187 | jsrt::context::scope scope(context); 188 | jsrt::value value = jsrt::boolean::create(true); 189 | jsrt::boolean boolean = jsrt::boolean::convert(value); 190 | Assert::AreEqual(value.type(), JsBoolean); 191 | Assert::IsTrue(boolean.data()); 192 | Assert::IsTrue(jsrt::boolean::true_value().data()); 193 | Assert::IsFalse(jsrt::boolean::false_value().data()); 194 | } 195 | runtime.dispose(); 196 | } 197 | 198 | MY_TEST_METHOD(number, "Test number methods.") 199 | { 200 | jsrt::runtime runtime = jsrt::runtime::create(); 201 | jsrt::context context = runtime.create_context(); 202 | { 203 | jsrt::context::scope scope(context); 204 | jsrt::value value = jsrt::number::create(1.0); 205 | jsrt::number number = jsrt::number::convert(value); 206 | Assert::AreEqual(value.type(), JsNumber); 207 | Assert::AreEqual(number.as_double(), 1.0); 208 | number = jsrt::number::create(42); 209 | Assert::AreEqual(number.as_int(), 42); 210 | } 211 | runtime.dispose(); 212 | } 213 | 214 | MY_TEST_METHOD(string, "Test string methods.") 215 | { 216 | jsrt::runtime runtime = jsrt::runtime::create(); 217 | jsrt::context context = runtime.create_context(); 218 | { 219 | jsrt::context::scope scope(context); 220 | jsrt::value value = jsrt::string::create(L"foo"); 221 | jsrt::string string = jsrt::string::convert(value); 222 | Assert::AreEqual(value.type(), JsString); 223 | Assert::AreEqual(string.data(), static_cast(L"foo")); 224 | Assert::AreEqual(string.length(), 3); 225 | } 226 | runtime.dispose(); 227 | } 228 | }; 229 | } -------------------------------------------------------------------------------- /win10/ie/jsrt-wrappers.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30110.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsrt-wrappers", "src\jsrt-wrappers.vcxproj", "{18B6A46C-9534-498D-8463-9822BC5CAC42}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsrt-wrappers-test", "test\jsrt-wrappers-test.vcxproj", "{3E7E9985-7F51-49BC-9B1F-794D27F860DB}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Win32 = Debug|Win32 13 | Debug|x64 = Debug|x64 14 | Release|Win32 = Release|Win32 15 | Release|x64 = Release|x64 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|Win32.Build.0 = Debug|Win32 20 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|x64.ActiveCfg = Debug|x64 21 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|x64.Build.0 = Debug|x64 22 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|Win32.ActiveCfg = Release|Win32 23 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|Win32.Build.0 = Release|Win32 24 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|x64.ActiveCfg = Release|x64 25 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|x64.Build.0 = Release|x64 26 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|Win32.ActiveCfg = Debug|Win32 27 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|Win32.Build.0 = Debug|Win32 28 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|x64.ActiveCfg = Debug|x64 29 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|x64.Build.0 = Debug|x64 30 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|Win32.ActiveCfg = Release|Win32 31 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|Win32.Build.0 = Release|Win32 32 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|x64.ActiveCfg = Release|x64 33 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|x64.Build.0 = Release|x64 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | EndGlobal 39 | -------------------------------------------------------------------------------- /win10/ie/src/jsrt-wrappers.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {18B6A46C-9534-498D-8463-9822BC5CAC42} 23 | Win32Proj 24 | jsrtwrappers 25 | 10.0.10240.0 26 | 27 | 28 | 29 | StaticLibrary 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | StaticLibrary 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | 43 | 44 | Use 45 | Level3 46 | Disabled 47 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 48 | true 49 | true 50 | 51 | 52 | Windows 53 | true 54 | 55 | 56 | true 57 | 58 | 59 | 60 | 61 | Level3 62 | Use 63 | MaxSpeed 64 | true 65 | true 66 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 67 | true 68 | 69 | 70 | Windows 71 | true 72 | true 73 | true 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Create 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /win10/ie/src/jsrt-wrappers.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | -------------------------------------------------------------------------------- /win10/ie/src/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | -------------------------------------------------------------------------------- /win10/ie/src/stdafx.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "targetver.h" 18 | #define WIN32_LEAN_AND_MEAN 19 | #include "jsrt.h" -------------------------------------------------------------------------------- /win10/ie/src/targetver.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | -------------------------------------------------------------------------------- /win10/ie/test/array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(array) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty array handle.") 26 | { 27 | jsrt::array<> handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::array<> array; 37 | TEST_NO_CONTEXT_CALL(jsrt::array<>::create(0)); 38 | TEST_NO_CONTEXT_CALL(array.size()); 39 | TEST_NO_CONTEXT_CALL(array[0]); 40 | TEST_NO_CONTEXT_CALL(array[0] = jsrt::value()); 41 | runtime.dispose(); 42 | } 43 | 44 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 45 | { 46 | jsrt::runtime runtime = jsrt::runtime::create(); 47 | jsrt::context context = runtime.create_context(); 48 | { 49 | jsrt::context::scope scope(context); 50 | jsrt::array<> array; 51 | TEST_NULL_ARG_CALL(array.size()); 52 | TEST_NULL_ARG_CALL((jsrt::value)array[0]); 53 | TEST_NULL_ARG_CALL(array[0] = jsrt::value()); 54 | } 55 | runtime.dispose(); 56 | } 57 | 58 | MY_TEST_METHOD(create, "Test ::create method.") 59 | { 60 | jsrt::runtime runtime = jsrt::runtime::create(); 61 | jsrt::context context = runtime.create_context(); 62 | { 63 | jsrt::context::scope scope(context); 64 | jsrt::value value = jsrt::array<>::create(0); 65 | jsrt::array<> array = (jsrt::array<>)value; 66 | 67 | jsrt::array array2 = jsrt::array::create({ 1, 2, 3, 4 }); 68 | Assert::AreEqual((double)array2[0], 1.0); 69 | Assert::AreEqual((double)array2[3], 4.0); 70 | } 71 | runtime.dispose(); 72 | } 73 | 74 | MY_TEST_METHOD(indexing, "Test indexing.") 75 | { 76 | jsrt::runtime runtime = jsrt::runtime::create(); 77 | jsrt::context context = runtime.create_context(); 78 | { 79 | jsrt::context::scope scope(context); 80 | jsrt::array darray = jsrt::array::create(1); 81 | darray[0] = 10; 82 | darray[1] = 20; 83 | Assert::AreEqual((double)darray[0], 10.0); 84 | Assert::AreEqual((double)darray[1], 20.0); 85 | 86 | jsrt::array barray = jsrt::array::create(1); 87 | barray[0] = true; 88 | barray[1] = true; 89 | Assert::IsTrue((bool) barray[0]); 90 | Assert::IsTrue((bool) barray[1]); 91 | 92 | jsrt::array sarray = jsrt::array::create(1); 93 | sarray[0] = L"foo"; 94 | sarray[1] = L"bar"; 95 | Assert::AreEqual((std::wstring) sarray[0], (std::wstring)L"foo"); 96 | Assert::AreEqual((std::wstring) sarray[1], (std::wstring)L"bar"); 97 | 98 | Assert::AreEqual(sarray.size(), 2); 99 | } 100 | runtime.dispose(); 101 | } 102 | }; 103 | } -------------------------------------------------------------------------------- /win10/ie/test/error.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(error) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty error handle.") 26 | { 27 | jsrt::error handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::error error; 37 | TEST_NO_CONTEXT_CALL(jsrt::error::create(L"foo")); 38 | runtime.dispose(); 39 | } 40 | 41 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 42 | { 43 | jsrt::runtime runtime = jsrt::runtime::create(); 44 | jsrt::context context = runtime.create_context(); 45 | { 46 | jsrt::context::scope scope(context); 47 | jsrt::error error; 48 | TEST_NULL_ARG_CALL(error.message()); 49 | } 50 | runtime.dispose(); 51 | } 52 | 53 | MY_TEST_METHOD(create, "Test ::create method.") 54 | { 55 | jsrt::runtime runtime = jsrt::runtime::create(); 56 | jsrt::context context = runtime.create_context(); 57 | { 58 | jsrt::context::scope scope(context); 59 | jsrt::value value = jsrt::error::create(L"foo"); 60 | jsrt::error error = (jsrt::error)value; 61 | } 62 | runtime.dispose(); 63 | } 64 | 65 | MY_TEST_METHOD(message, "Test messages.") 66 | { 67 | jsrt::runtime runtime = jsrt::runtime::create(); 68 | jsrt::context context = runtime.create_context(); 69 | { 70 | jsrt::context::scope scope(context); 71 | jsrt::value value = jsrt::error::create(L"foo"); 72 | jsrt::error error = (jsrt::error)value; 73 | Assert::AreEqual(error.name(), (std::wstring)L"Error"); 74 | Assert::AreEqual(error.message(), (std::wstring)L"foo"); 75 | 76 | error = jsrt::error::create(L"%s %d", L"foo", 20); 77 | Assert::AreEqual(error.message(), (std::wstring)L"foo 20"); 78 | } 79 | runtime.dispose(); 80 | } 81 | 82 | MY_TEST_METHOD(specialized, "Test specialize errors.") 83 | { 84 | jsrt::runtime runtime = jsrt::runtime::create(); 85 | jsrt::context context = runtime.create_context(); 86 | { 87 | jsrt::context::scope scope(context); 88 | jsrt::error error; 89 | 90 | error = jsrt::error::create_range_error(L""); 91 | Assert::AreEqual(error.name(), (std::wstring)L"RangeError"); 92 | 93 | error = jsrt::error::create_reference_error(L""); 94 | Assert::AreEqual(error.name(), (std::wstring)L"ReferenceError"); 95 | 96 | error = jsrt::error::create_syntax_error(L""); 97 | Assert::AreEqual(error.name(), (std::wstring)L"SyntaxError"); 98 | 99 | error = jsrt::error::create_type_error(L""); 100 | Assert::AreEqual(error.name(), (std::wstring)L"TypeError"); 101 | 102 | error = jsrt::error::create_uri_error(L""); 103 | Assert::AreEqual(error.name(), (std::wstring)L"URIError"); 104 | } 105 | runtime.dispose(); 106 | } 107 | }; 108 | } -------------------------------------------------------------------------------- /win10/ie/test/jsrt-wrappers-test.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB} 23 | Win32Proj 24 | jsrtwrapperstest 25 | 10.0.10240.0 26 | 27 | 28 | 29 | DynamicLibrary 30 | true 31 | v140 32 | Unicode 33 | false 34 | 35 | 36 | DynamicLibrary 37 | false 38 | v140 39 | true 40 | Unicode 41 | false 42 | 43 | 44 | 45 | true 46 | 47 | 48 | 49 | Use 50 | Level3 51 | Disabled 52 | $(VCInstallDir)UnitTest\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) 53 | WIN32;_DEBUG;%(PreprocessorDefinitions) 54 | true 55 | true 56 | 57 | 58 | Windows 59 | true 60 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 61 | kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;jsrt.lib;%(AdditionalDependencies) 62 | 63 | 64 | true 65 | 66 | 67 | 68 | 69 | Level3 70 | Use 71 | MaxSpeed 72 | true 73 | true 74 | $(VCInstallDir)UnitTest\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) 75 | WIN32;NDEBUG;%(PreprocessorDefinitions) 76 | true 77 | 78 | 79 | Windows 80 | true 81 | true 82 | true 83 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 84 | jsrt.lib;%(AdditionalDependencies) 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | Create 108 | 109 | 110 | 111 | 112 | 113 | 114 | {18b6a46c-9534-498d-8463-9822bc5cac42} 115 | 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /win10/ie/test/jsrt-wrappers-test.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | Source Files 64 | 65 | 66 | Source Files 67 | 68 | 69 | Source Files 70 | 71 | 72 | Source Files 73 | 74 | 75 | Source Files 76 | 77 | 78 | -------------------------------------------------------------------------------- /win10/ie/test/optional.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(optional) 24 | { 25 | public: 26 | MY_TEST_METHOD(optional_values, "Test optional.") 27 | { 28 | jsrt::optional o1; 29 | Assert::IsFalse(o1.has_value()); 30 | Assert::AreEqual(o1.value(), 0); 31 | o1.clear(); 32 | o1 = 10; 33 | Assert::IsTrue(o1.has_value()); 34 | Assert::AreEqual(o1.value(), 10); 35 | o1.clear(); 36 | Assert::IsFalse(o1.has_value()); 37 | Assert::AreEqual(o1.value(), 0); 38 | o1 = jsrt::missing(); 39 | Assert::IsFalse(o1.has_value()); 40 | Assert::AreEqual(o1.value(), 0); 41 | } 42 | }; 43 | } -------------------------------------------------------------------------------- /win10/ie/test/pinned.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(pinned) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_handle, "Test a pinned empty handle.") 27 | { 28 | jsrt::pinned handle; 29 | Assert::AreEqual(handle->handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(handle->is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(context_handle, "Test a pinned context handle.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::pinned context = runtime.create_context(); 37 | Assert::IsTrue(context->is_valid()); 38 | context.release(); 39 | Assert::IsFalse(context->is_valid()); 40 | runtime.dispose(); 41 | } 42 | 43 | MY_TEST_METHOD(handle_refcounting, "Test refcounting on a pinned handle.") 44 | { 45 | jsrt::runtime runtime = jsrt::runtime::create(); 46 | jsrt::context context = runtime.create_context(); 47 | { 48 | jsrt::pinned pinned_context = context; 49 | Assert::AreEqual(context.add_reference(), (unsigned int) 2); 50 | jsrt::pinned another_pinned_context = pinned_context; 51 | jsrt::pinned yet_another_pinned_context = std::move(pinned_context); 52 | Assert::IsTrue(yet_another_pinned_context->is_valid()); 53 | Assert::AreEqual(context.release(), (unsigned int) 2); 54 | } 55 | Assert::AreEqual(context.add_reference(), (unsigned int) 1); 56 | context.release(); 57 | runtime.dispose(); 58 | } 59 | }; 60 | } -------------------------------------------------------------------------------- /win10/ie/test/profiler.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | class Profiler sealed : public IActiveScriptProfilerCallback2 18 | { 19 | private: 20 | long m_refCount; 21 | 22 | public: 23 | Profiler(void) 24 | { 25 | m_refCount = 1; 26 | } 27 | 28 | ~Profiler(void) 29 | { 30 | } 31 | 32 | // IUnknown 33 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObj) 34 | { 35 | if (riid == IID_IUnknown) 36 | { 37 | *ppvObj = (IUnknown *) this; 38 | } 39 | else if (riid == IID_IActiveScriptProfilerCallback) 40 | { 41 | *ppvObj = (IActiveScriptProfilerCallback *) this; 42 | } 43 | else if (riid == IID_IActiveScriptProfilerCallback2) 44 | { 45 | *ppvObj = (IActiveScriptProfilerCallback2 *) this; 46 | } 47 | else 48 | { 49 | *ppvObj = NULL; 50 | return E_NOINTERFACE; 51 | } 52 | 53 | 54 | AddRef(); 55 | return NOERROR; 56 | } 57 | 58 | ULONG STDMETHODCALLTYPE AddRef(void) 59 | { 60 | return InterlockedIncrement(&m_refCount); 61 | } 62 | 63 | ULONG STDMETHODCALLTYPE Release(void) 64 | { 65 | long lw; 66 | 67 | 68 | if (0 == (lw = InterlockedDecrement(&m_refCount))) 69 | { 70 | delete this; 71 | } 72 | return lw; 73 | } 74 | 75 | // IActiveScriptProfilerCallback 76 | HRESULT STDMETHODCALLTYPE Initialize(DWORD dwContext) { return S_OK; } 77 | HRESULT STDMETHODCALLTYPE Shutdown(HRESULT hrReason) { return S_OK; } 78 | HRESULT STDMETHODCALLTYPE ScriptCompiled(PROFILER_TOKEN scriptId, PROFILER_SCRIPT_TYPE type, IUnknown *pIDebugDocumentContext) { return S_OK; } 79 | HRESULT STDMETHODCALLTYPE FunctionCompiled(PROFILER_TOKEN functionId, PROFILER_TOKEN scriptId, const wchar_t *pwszFunctionName, const wchar_t *pwszFunctionNameHint, IUnknown *pIDebugDocumentContext) { return S_OK; } 80 | HRESULT STDMETHODCALLTYPE OnFunctionEnter(PROFILER_TOKEN scriptId, PROFILER_TOKEN functionId) { return S_OK; } 81 | HRESULT STDMETHODCALLTYPE OnFunctionExit(PROFILER_TOKEN scriptId, PROFILER_TOKEN functionId) { return S_OK; } 82 | 83 | // IActiveScriptProfilerCallback2 84 | HRESULT STDMETHODCALLTYPE OnFunctionEnterByName(const wchar_t *pwszFunctionName, PROFILER_SCRIPT_TYPE type) { return S_OK; } 85 | HRESULT STDMETHODCALLTYPE OnFunctionExitByName(const wchar_t *pwszFunctionName, PROFILER_SCRIPT_TYPE type) { return S_OK; } 86 | }; 87 | -------------------------------------------------------------------------------- /win10/ie/test/property_descriptor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(property_descriptor) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_handle, "Test an empty property_descriptor handle.") 27 | { 28 | jsrt::property_descriptor handle; 29 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(handle.is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(no_context, "Test calls with no context.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::context context = runtime.create_context(); 37 | jsrt::property_descriptor property_descriptor; 38 | TEST_NO_CONTEXT_CALL(jsrt::property_descriptor::create()); 39 | TEST_NO_CONTEXT_CALL(jsrt::property_descriptor::create(jsrt::function(), jsrt::function())); 40 | TEST_NO_CONTEXT_CALL(property_descriptor.writable()); 41 | TEST_NO_CONTEXT_CALL(property_descriptor.set_writable(true)); 42 | TEST_NO_CONTEXT_CALL(property_descriptor.enumerable()); 43 | TEST_NO_CONTEXT_CALL(property_descriptor.set_enumerable(true)); 44 | TEST_NO_CONTEXT_CALL(property_descriptor.configurable()); 45 | TEST_NO_CONTEXT_CALL(property_descriptor.set_configurable(true)); 46 | TEST_NO_CONTEXT_CALL(property_descriptor.value()); 47 | TEST_NO_CONTEXT_CALL(property_descriptor.set_value(true)); 48 | TEST_NO_CONTEXT_CALL(property_descriptor.getter()); 49 | TEST_NO_CONTEXT_CALL(property_descriptor.set_getter(jsrt::function())); 50 | TEST_NO_CONTEXT_CALL(property_descriptor.setter()); 51 | TEST_NO_CONTEXT_CALL(property_descriptor.set_setter(jsrt::function())); 52 | runtime.dispose(); 53 | } 54 | 55 | static bool b(const jsrt::call_info &info) 56 | { 57 | return true; 58 | } 59 | 60 | static void set_b(const jsrt::call_info &info, bool v) 61 | { 62 | Assert::IsTrue(v); 63 | } 64 | 65 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 66 | { 67 | jsrt::runtime runtime = jsrt::runtime::create(); 68 | jsrt::context context = runtime.create_context(); 69 | { 70 | jsrt::context::scope scope(context); 71 | jsrt::property_descriptor property_descriptor; 72 | TEST_NULL_ARG_CALL(property_descriptor.writable()); 73 | TEST_NULL_ARG_CALL(property_descriptor.set_writable(true)); 74 | TEST_NULL_ARG_CALL(property_descriptor.enumerable()); 75 | TEST_NULL_ARG_CALL(property_descriptor.set_enumerable(true)); 76 | TEST_NULL_ARG_CALL(property_descriptor.configurable()); 77 | TEST_NULL_ARG_CALL(property_descriptor.set_configurable(true)); 78 | TEST_NULL_ARG_CALL(property_descriptor.value()); 79 | TEST_NULL_ARG_CALL(property_descriptor.set_value(true)); 80 | TEST_NULL_ARG_CALL(property_descriptor.getter()); 81 | TEST_NULL_ARG_CALL(property_descriptor.set_getter(jsrt::function())); 82 | TEST_NULL_ARG_CALL(property_descriptor.setter()); 83 | TEST_NULL_ARG_CALL(property_descriptor.set_setter(jsrt::function())); 84 | 85 | jsrt::property_descriptor valid_descriptor = jsrt::property_descriptor::create(); 86 | TEST_NULL_ARG_CALL(jsrt::property_descriptor::create(jsrt::function(), jsrt::function())); 87 | TEST_NULL_ARG_CALL(jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function())); 88 | TEST_NULL_ARG_CALL(property_descriptor.set_getter(jsrt::function())); 89 | TEST_NULL_ARG_CALL(property_descriptor.set_setter(jsrt::function())); 90 | } 91 | runtime.dispose(); 92 | } 93 | 94 | MY_TEST_METHOD(create, "Test ::create method.") 95 | { 96 | jsrt::runtime runtime = jsrt::runtime::create(); 97 | jsrt::context context = runtime.create_context(); 98 | { 99 | jsrt::context::scope scope(context); 100 | jsrt::value value = jsrt::property_descriptor::create(); 101 | jsrt::property_descriptor property_descriptor = (jsrt::property_descriptor)value; 102 | Assert::IsTrue(property_descriptor.is_valid()); 103 | property_descriptor = jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function::create(set_b)); 104 | Assert::IsTrue(property_descriptor.is_valid()); 105 | } 106 | runtime.dispose(); 107 | } 108 | 109 | MY_TEST_METHOD(descriptors, "Test descriptor methods.") 110 | { 111 | jsrt::runtime runtime = jsrt::runtime::create(); 112 | jsrt::context context = runtime.create_context(); 113 | { 114 | jsrt::context::scope scope(context); 115 | jsrt::object object = jsrt::object::create(); 116 | 117 | jsrt::property_descriptor desc = jsrt::property_descriptor::create(); 118 | desc.set_configurable(false); 119 | desc.set_enumerable(false); 120 | desc.set_writable(false); 121 | desc.set_value(10); 122 | object.define_property(jsrt::property_id::create(L"a"), desc); 123 | Assert::AreEqual(object.get_property(jsrt::property_id::create(L"a")), 10.0); 124 | desc = object.get_own_property_descriptor(jsrt::property_id::create(L"a")); 125 | Assert::IsFalse(desc.configurable()); 126 | Assert::IsFalse(desc.enumerable()); 127 | Assert::IsFalse(desc.writable()); 128 | Assert::AreEqual(desc.value(), 10.0); 129 | jsrt::property_descriptor bdesc = jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function::create(set_b)); 130 | object.define_property(jsrt::property_id::create(L"b"), bdesc); 131 | Assert::IsTrue(object.get_property(jsrt::property_id::create(L"b"))); 132 | object.set_property(jsrt::property_id::create(L"b"), true); 133 | } 134 | runtime.dispose(); 135 | } 136 | }; 137 | } -------------------------------------------------------------------------------- /win10/ie/test/property_id.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(property_id) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_id, "Test an empty property ID.") 27 | { 28 | jsrt::property_id id; 29 | Assert::AreEqual(id.handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(id.is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(name, "Test a property ID.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::context context = runtime.create_context(); 37 | { 38 | jsrt::context::scope scope(context); 39 | jsrt::property_id id = jsrt::property_id::create(L"foo"); 40 | Assert::AreEqual(id.name(), (std::wstring) L"foo"); 41 | } 42 | runtime.dispose(); 43 | } 44 | 45 | MY_TEST_METHOD(invalid, "Test property ID methods with invalid ID.") 46 | { 47 | jsrt::runtime runtime = jsrt::runtime::create(); 48 | jsrt::context context = runtime.create_context(); 49 | { 50 | jsrt::context::scope scope(context); 51 | try 52 | { 53 | jsrt::property_id id; 54 | id.name(); 55 | Assert::Fail(); 56 | } 57 | catch (jsrt::invalid_argument_exception) 58 | { 59 | } 60 | } 61 | runtime.dispose(); 62 | } 63 | 64 | MY_TEST_METHOD(no_context, "Test property ID methods with no context.") 65 | { 66 | try 67 | { 68 | jsrt::property_id::create(L"foo"); 69 | Assert::Fail(); 70 | } 71 | catch (jsrt::no_current_context_exception) 72 | { 73 | } 74 | 75 | try 76 | { 77 | jsrt::property_id foo; 78 | foo.name(); 79 | Assert::Fail(); 80 | } 81 | catch (jsrt::no_current_context_exception) 82 | { 83 | } 84 | } 85 | }; 86 | } -------------------------------------------------------------------------------- /win10/ie/test/readme.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(readme) 23 | { 24 | public: 25 | static double add(const jsrt::call_info &info, double a, double b) 26 | { 27 | return a + b; 28 | } 29 | 30 | MY_TEST_METHOD(samples, "Test readme samples.") 31 | { 32 | jsrt::runtime runtime = jsrt::runtime::create(); 33 | jsrt::context context = runtime.create_context(); 34 | { 35 | jsrt::context::scope scope(context); 36 | jsrt::pinned pinned_obj = jsrt::object::create(); 37 | 38 | jsrt::object obj = jsrt::object::create(); 39 | obj.set_property(jsrt::property_id::create(L"boolProperty"), true); 40 | bool bool_value = obj.get_property(jsrt::property_id::create(L"boolProperty")); 41 | obj.set_property(jsrt::property_id::create(L"stringProperty"), L"foo"); 42 | 43 | jsrt::array darray = jsrt::array::create(1); 44 | darray[0] = 10; 45 | darray[1] = 20; 46 | 47 | auto f = (jsrt::function)jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;"); 48 | double a = f(jsrt::context::undefined(), 1, 2); 49 | 50 | auto nf = jsrt::function::create(add); 51 | jsrt::context::global().set_property(jsrt::property_id::create(L"add"), nf); 52 | jsrt::context::run(L"add(1, 2)"); 53 | 54 | auto bf = jsrt::bound_function( 55 | jsrt::context::undefined(), 56 | (jsrt::function)jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;")); 57 | double ba = bf(1, 2); 58 | } 59 | runtime.dispose(); 60 | } 61 | }; 62 | } -------------------------------------------------------------------------------- /win10/ie/test/reference.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(reference) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty handle.") 26 | { 27 | jsrt::reference handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(context_handle, "Test a context handle.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | Assert::IsTrue(context.is_valid()); 37 | runtime.dispose(); 38 | } 39 | 40 | MY_TEST_METHOD(handle_refcounting, "Test refcounting on a handle.") 41 | { 42 | jsrt::runtime runtime = jsrt::runtime::create(); 43 | jsrt::context context = runtime.create_context(); 44 | Assert::AreEqual(context.add_reference(), (unsigned int) 1); 45 | Assert::AreEqual(context.release(), (unsigned int) 0); 46 | runtime.dispose(); 47 | } 48 | 49 | MY_TEST_METHOD(invalid_handle, "Test refcounting on an invalid handle.") 50 | { 51 | jsrt::context context; 52 | 53 | try 54 | { 55 | context.add_reference(); 56 | Assert::Fail(); 57 | } 58 | catch (jsrt::null_argument_exception) 59 | { 60 | } 61 | 62 | try 63 | { 64 | context.release(); 65 | Assert::Fail(); 66 | } 67 | catch (jsrt::null_argument_exception) 68 | { 69 | } 70 | } 71 | }; 72 | } -------------------------------------------------------------------------------- /win10/ie/test/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | -------------------------------------------------------------------------------- /win10/ie/test/stdafx.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "targetver.h" 18 | #define WIN32_LEAN_AND_MEAN 19 | #include "jsrt.h" 20 | #include "jsrt-wrappers.h" 21 | #include "profiler.h" 22 | #include "CppUnitTest.h" 23 | 24 | #define MY_TEST_METHOD(name, description) \ 25 | BEGIN_TEST_METHOD_ATTRIBUTE(name) \ 26 | TEST_DESCRIPTION(description) \ 27 | END_TEST_METHOD_ATTRIBUTE() \ 28 | \ 29 | TEST_METHOD(name) 30 | 31 | #define MY_TEST_METHOD_DISABLED(name, description) \ 32 | BEGIN_TEST_METHOD_ATTRIBUTE(name) \ 33 | TEST_DESCRIPTION(description) \ 34 | TEST_IGNORE() \ 35 | END_TEST_METHOD_ATTRIBUTE() \ 36 | \ 37 | TEST_METHOD(name) 38 | 39 | #define TEST_FAILED_CALL(call, exception) \ 40 | try \ 41 | { \ 42 | call; \ 43 | Assert::Fail(); \ 44 | } \ 45 | catch (const jsrt:: exception &) \ 46 | { \ 47 | } 48 | 49 | #define TEST_INVALID_ARG_CALL(call) \ 50 | TEST_FAILED_CALL(call, invalid_argument_exception) 51 | 52 | #define TEST_NO_CONTEXT_CALL(call) \ 53 | TEST_FAILED_CALL(call, no_current_context_exception) 54 | 55 | #define TEST_NULL_ARG_CALL(call) \ 56 | TEST_FAILED_CALL(call, null_argument_exception) 57 | 58 | #define TEST_SCRIPT_EXCEPTION_CALL(call) \ 59 | TEST_FAILED_CALL(call, script_exception) 60 | 61 | #define IfComFailError(v) \ 62 | { \ 63 | hr = (v); \ 64 | if (FAILED(hr)) \ 65 | { \ 66 | goto error; \ 67 | } \ 68 | } 69 | 70 | extern IDebugApplication *get_debug_application(); 71 | 72 | template <> 73 | static std::wstring Microsoft::VisualStudio::CppUnitTestFramework::ToString(const JsValueType& q) 74 | { 75 | switch (q) 76 | { 77 | case JsUndefined: 78 | return L"JsUndefined"; 79 | case JsNull: 80 | return L"JsNull"; 81 | case JsNumber: 82 | return L"JsNumber"; 83 | case JsString: 84 | return L"JsString"; 85 | case JsBoolean: 86 | return L"JsBoolean"; 87 | case JsObject: 88 | return L"JsObject"; 89 | case JsFunction: 90 | return L"JsFunction"; 91 | case JsError: 92 | return L"JsError"; 93 | case JsArray: 94 | return L"JsArray"; 95 | default: 96 | return std::wstring(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /win10/ie/test/targetver.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | -------------------------------------------------------------------------------- /win10/ie/test/value.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(value) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty value handle.") 26 | { 27 | jsrt::value handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | 31 | jsrt::boolean boolean_handle; 32 | Assert::AreEqual(boolean_handle.handle(), JS_INVALID_REFERENCE); 33 | Assert::IsFalse(boolean_handle.is_valid()); 34 | 35 | jsrt::number number_handle; 36 | Assert::AreEqual(number_handle.handle(), JS_INVALID_REFERENCE); 37 | Assert::IsFalse(number_handle.is_valid()); 38 | 39 | jsrt::string string_handle; 40 | Assert::AreEqual(string_handle.handle(), JS_INVALID_REFERENCE); 41 | Assert::IsFalse(string_handle.is_valid()); 42 | } 43 | 44 | MY_TEST_METHOD(no_context, "Test calls with no context.") 45 | { 46 | jsrt::runtime runtime = jsrt::runtime::create(); 47 | jsrt::context context = runtime.create_context(); 48 | jsrt::value value; 49 | VARIANT v; 50 | TEST_NO_CONTEXT_CALL(value.type()); 51 | TEST_NO_CONTEXT_CALL(value.to_variant(&v)); 52 | TEST_NO_CONTEXT_CALL(jsrt::value::from_variant(&v)); 53 | TEST_NO_CONTEXT_CALL(jsrt::boolean::create(true)); 54 | TEST_NO_CONTEXT_CALL(jsrt::boolean::true_value()); 55 | TEST_NO_CONTEXT_CALL(jsrt::boolean::false_value()); 56 | TEST_NO_CONTEXT_CALL(((jsrt::boolean)value).data()); 57 | TEST_NO_CONTEXT_CALL(jsrt::number::create(1.0)); 58 | TEST_NO_CONTEXT_CALL(((jsrt::number)value).data()); 59 | TEST_NO_CONTEXT_CALL(jsrt::string::create(L"foo")); 60 | TEST_NO_CONTEXT_CALL(((jsrt::string)value).data()); 61 | TEST_NO_CONTEXT_CALL(((jsrt::string)value).length()); 62 | runtime.dispose(); 63 | } 64 | 65 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 66 | { 67 | jsrt::runtime runtime = jsrt::runtime::create(); 68 | jsrt::context context = runtime.create_context(); 69 | { 70 | jsrt::context::scope scope(context); 71 | jsrt::value value; 72 | VARIANT v; 73 | TEST_NULL_ARG_CALL(value.type()); 74 | TEST_NULL_ARG_CALL(value.to_variant(&v)); 75 | jsrt::boolean boolean; 76 | TEST_NULL_ARG_CALL(boolean.data()); 77 | jsrt::number number; 78 | TEST_NULL_ARG_CALL(number.data()); 79 | jsrt::string string; 80 | TEST_NULL_ARG_CALL(string.data()); 81 | TEST_NULL_ARG_CALL(string.length()); 82 | } 83 | runtime.dispose(); 84 | } 85 | 86 | MY_TEST_METHOD(type, "Test the ::type method.") 87 | { 88 | jsrt::runtime runtime = jsrt::runtime::create(); 89 | jsrt::context context = runtime.create_context(); 90 | { 91 | jsrt::context::scope scope(context); 92 | jsrt::value value; 93 | value = jsrt::context::undefined(); 94 | Assert::AreEqual(value.type(), JsUndefined); 95 | value = jsrt::context::null(); 96 | Assert::AreEqual(value.type(), JsNull); 97 | value = jsrt::boolean::true_value(); 98 | Assert::AreEqual(value.type(), JsBoolean); 99 | value = jsrt::number::create(10); 100 | Assert::AreEqual(value.type(), JsNumber); 101 | value = jsrt::string::create(L"foo"); 102 | Assert::AreEqual(value.type(), JsString); 103 | value = jsrt::object::create(); 104 | Assert::AreEqual(value.type(), JsObject); 105 | value = jsrt::context::parse(L"1 + 2;"); 106 | Assert::AreEqual(value.type(), JsFunction); 107 | value = jsrt::error::create_uri_error(L"foo"); 108 | Assert::AreEqual(value.type(), JsError); 109 | value = jsrt::array::create(10); 110 | Assert::AreEqual(value.type(), JsArray); 111 | } 112 | runtime.dispose(); 113 | } 114 | 115 | MY_TEST_METHOD(variant, "Test the ::to_variant and ::from_variant methods.") 116 | { 117 | jsrt::runtime runtime = jsrt::runtime::create(); 118 | jsrt::context context = runtime.create_context(); 119 | { 120 | jsrt::context::scope scope(context); 121 | VARIANT v; 122 | v.vt = VT_I4; 123 | v.intVal = 42; 124 | jsrt::value value = jsrt::value::from_variant(&v); 125 | Assert::AreEqual((int) value.type(), (int) JsNumber); 126 | Assert::AreEqual(((jsrt::number) value).data(), 42.0); 127 | value = jsrt::number::create(32); 128 | value.to_variant(&v); 129 | Assert::AreEqual((int) v.vt, (int) VT_I4); 130 | Assert::AreEqual(v.intVal, 32); 131 | } 132 | runtime.dispose(); 133 | } 134 | 135 | MY_TEST_METHOD(equals, "Test the ::equals and ::strict_equals methods.") 136 | { 137 | jsrt::runtime runtime = jsrt::runtime::create(); 138 | jsrt::context context = runtime.create_context(); 139 | { 140 | jsrt::context::scope scope(context); 141 | jsrt::value v1 = jsrt::number::create(1); 142 | jsrt::value v2 = jsrt::string::create(L"1"); 143 | jsrt::value v3 = jsrt::number::create(1); 144 | Assert::IsTrue(v1.equals(v2)); 145 | Assert::IsTrue(v1.equals(v3)); 146 | Assert::IsFalse(v1.strict_equals(v2)); 147 | Assert::IsTrue(v1.strict_equals(v3)); 148 | } 149 | runtime.dispose(); 150 | } 151 | 152 | MY_TEST_METHOD(boolean, "Test boolean methods.") 153 | { 154 | jsrt::runtime runtime = jsrt::runtime::create(); 155 | jsrt::context context = runtime.create_context(); 156 | { 157 | jsrt::context::scope scope(context); 158 | jsrt::value value = jsrt::boolean::create(true); 159 | jsrt::boolean boolean = jsrt::boolean::convert(value); 160 | Assert::AreEqual(value.type(), JsBoolean); 161 | Assert::IsTrue(boolean.data()); 162 | Assert::IsTrue(jsrt::boolean::true_value().data()); 163 | Assert::IsFalse(jsrt::boolean::false_value().data()); 164 | } 165 | runtime.dispose(); 166 | } 167 | 168 | MY_TEST_METHOD(number, "Test number methods.") 169 | { 170 | jsrt::runtime runtime = jsrt::runtime::create(); 171 | jsrt::context context = runtime.create_context(); 172 | { 173 | jsrt::context::scope scope(context); 174 | jsrt::value value = jsrt::number::create(1.0); 175 | jsrt::number number = jsrt::number::convert(value); 176 | Assert::AreEqual(value.type(), JsNumber); 177 | Assert::AreEqual(number.data(), 1.0); 178 | number = jsrt::number::create(42); 179 | Assert::AreEqual(number.data(), 42.0); 180 | } 181 | runtime.dispose(); 182 | } 183 | 184 | MY_TEST_METHOD(string, "Test string methods.") 185 | { 186 | jsrt::runtime runtime = jsrt::runtime::create(); 187 | jsrt::context context = runtime.create_context(); 188 | { 189 | jsrt::context::scope scope(context); 190 | jsrt::value value = jsrt::string::create(L"foo"); 191 | jsrt::string string = jsrt::string::convert(value); 192 | Assert::AreEqual(value.type(), JsString); 193 | Assert::AreEqual(string.data(), (std::wstring) L"foo"); 194 | Assert::AreEqual(string.length(), 3); 195 | } 196 | runtime.dispose(); 197 | } 198 | }; 199 | } -------------------------------------------------------------------------------- /win8.1/jsrt-wrappers.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30110.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsrt-wrappers", "src\jsrt-wrappers.vcxproj", "{18B6A46C-9534-498D-8463-9822BC5CAC42}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsrt-wrappers-test", "test\jsrt-wrappers-test.vcxproj", "{3E7E9985-7F51-49BC-9B1F-794D27F860DB}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Win32 = Debug|Win32 13 | Debug|x64 = Debug|x64 14 | Release|Win32 = Release|Win32 15 | Release|x64 = Release|x64 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|Win32.Build.0 = Debug|Win32 20 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|x64.ActiveCfg = Debug|x64 21 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Debug|x64.Build.0 = Debug|x64 22 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|Win32.ActiveCfg = Release|Win32 23 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|Win32.Build.0 = Release|Win32 24 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|x64.ActiveCfg = Release|x64 25 | {18B6A46C-9534-498D-8463-9822BC5CAC42}.Release|x64.Build.0 = Release|x64 26 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|Win32.ActiveCfg = Debug|Win32 27 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|Win32.Build.0 = Debug|Win32 28 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|x64.ActiveCfg = Debug|x64 29 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Debug|x64.Build.0 = Debug|x64 30 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|Win32.ActiveCfg = Release|Win32 31 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|Win32.Build.0 = Release|Win32 32 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|x64.ActiveCfg = Release|x64 33 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB}.Release|x64.Build.0 = Release|x64 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | EndGlobal 39 | -------------------------------------------------------------------------------- /win8.1/src/jsrt-wrappers.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {18B6A46C-9534-498D-8463-9822BC5CAC42} 23 | Win32Proj 24 | jsrtwrappers 25 | 26 | 27 | 28 | StaticLibrary 29 | true 30 | v120 31 | Unicode 32 | 33 | 34 | StaticLibrary 35 | false 36 | v120 37 | true 38 | Unicode 39 | 40 | 41 | 42 | 43 | Use 44 | Level3 45 | Disabled 46 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 47 | true 48 | true 49 | 50 | 51 | Windows 52 | true 53 | 54 | 55 | true 56 | 57 | 58 | 59 | 60 | Level3 61 | Use 62 | MaxSpeed 63 | true 64 | true 65 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 66 | true 67 | 68 | 69 | Windows 70 | true 71 | true 72 | true 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | Create 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /win8.1/src/jsrt-wrappers.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | -------------------------------------------------------------------------------- /win8.1/src/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | -------------------------------------------------------------------------------- /win8.1/src/stdafx.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "targetver.h" 18 | #define WIN32_LEAN_AND_MEAN 19 | #include "jsrt.h" -------------------------------------------------------------------------------- /win8.1/src/targetver.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | -------------------------------------------------------------------------------- /win8.1/test/array.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(array) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty array handle.") 26 | { 27 | jsrt::array<> handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::array<> array; 37 | TEST_NO_CONTEXT_CALL(jsrt::array<>::create(0)); 38 | TEST_NO_CONTEXT_CALL(array.length()); 39 | TEST_NO_CONTEXT_CALL(array[0]); 40 | TEST_NO_CONTEXT_CALL(array[0] = jsrt::value()); 41 | runtime.dispose(); 42 | } 43 | 44 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 45 | { 46 | jsrt::runtime runtime = jsrt::runtime::create(); 47 | jsrt::context context = runtime.create_context(); 48 | { 49 | jsrt::context::scope scope(context); 50 | jsrt::array<> array; 51 | TEST_NULL_ARG_CALL(array.length()); 52 | TEST_NULL_ARG_CALL((jsrt::value)array[0]); 53 | TEST_NULL_ARG_CALL(array[0] = jsrt::value()); 54 | } 55 | runtime.dispose(); 56 | } 57 | 58 | MY_TEST_METHOD(create, "Test ::create method.") 59 | { 60 | jsrt::runtime runtime = jsrt::runtime::create(); 61 | jsrt::context context = runtime.create_context(); 62 | { 63 | jsrt::context::scope scope(context); 64 | jsrt::value value = jsrt::array<>::create(0); 65 | jsrt::array<> array = (jsrt::array<>)value; 66 | 67 | jsrt::array array2 = jsrt::array::create({ 1, 2, 3, 4 }); 68 | Assert::AreEqual((double)array2[0], 1.0); 69 | Assert::AreEqual((double)array2[3], 4.0); 70 | } 71 | runtime.dispose(); 72 | } 73 | 74 | MY_TEST_METHOD(indexing, "Test indexing.") 75 | { 76 | jsrt::runtime runtime = jsrt::runtime::create(); 77 | jsrt::context context = runtime.create_context(); 78 | { 79 | jsrt::context::scope scope(context); 80 | jsrt::array darray = jsrt::array::create(1); 81 | darray[0] = 10; 82 | darray[1] = 20; 83 | Assert::AreEqual((double)darray[0], 10.0); 84 | Assert::AreEqual((double)darray[1], 20.0); 85 | 86 | jsrt::array barray = jsrt::array::create(1); 87 | barray[0] = true; 88 | barray[1] = true; 89 | Assert::IsTrue((bool) barray[0]); 90 | Assert::IsTrue((bool) barray[1]); 91 | 92 | jsrt::array sarray = jsrt::array::create(1); 93 | sarray[0] = L"foo"; 94 | sarray[1] = L"bar"; 95 | Assert::AreEqual((std::wstring) sarray[0], (std::wstring)L"foo"); 96 | Assert::AreEqual((std::wstring) sarray[1], (std::wstring)L"bar"); 97 | 98 | Assert::AreEqual(sarray.length(), 2); 99 | } 100 | runtime.dispose(); 101 | } 102 | }; 103 | } -------------------------------------------------------------------------------- /win8.1/test/error.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(error) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty error handle.") 26 | { 27 | jsrt::error handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(no_context, "Test calls with no context.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | jsrt::error error; 37 | TEST_NO_CONTEXT_CALL(jsrt::error::create(L"foo")); 38 | runtime.dispose(); 39 | } 40 | 41 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 42 | { 43 | jsrt::runtime runtime = jsrt::runtime::create(); 44 | jsrt::context context = runtime.create_context(); 45 | { 46 | jsrt::context::scope scope(context); 47 | jsrt::error error; 48 | TEST_NULL_ARG_CALL(error.message()); 49 | } 50 | runtime.dispose(); 51 | } 52 | 53 | MY_TEST_METHOD(create, "Test ::create method.") 54 | { 55 | jsrt::runtime runtime = jsrt::runtime::create(); 56 | jsrt::context context = runtime.create_context(); 57 | { 58 | jsrt::context::scope scope(context); 59 | jsrt::value value = jsrt::error::create(L"foo"); 60 | jsrt::error error = (jsrt::error)value; 61 | } 62 | runtime.dispose(); 63 | } 64 | 65 | MY_TEST_METHOD(message, "Test messages.") 66 | { 67 | jsrt::runtime runtime = jsrt::runtime::create(); 68 | jsrt::context context = runtime.create_context(); 69 | { 70 | jsrt::context::scope scope(context); 71 | jsrt::value value = jsrt::error::create(L"foo"); 72 | jsrt::error error = (jsrt::error)value; 73 | Assert::AreEqual(error.name(), (std::wstring)L"Error"); 74 | Assert::AreEqual(error.message(), (std::wstring)L"foo"); 75 | 76 | error = jsrt::error::create(L"%s %d", L"foo", 20); 77 | Assert::AreEqual(error.message(), (std::wstring)L"foo 20"); 78 | } 79 | runtime.dispose(); 80 | } 81 | 82 | MY_TEST_METHOD(specialized, "Test specialize errors.") 83 | { 84 | jsrt::runtime runtime = jsrt::runtime::create(); 85 | jsrt::context context = runtime.create_context(); 86 | { 87 | jsrt::context::scope scope(context); 88 | jsrt::error error; 89 | 90 | error = jsrt::error::create_range_error(L""); 91 | Assert::AreEqual(error.name(), (std::wstring)L"RangeError"); 92 | 93 | error = jsrt::error::create_reference_error(L""); 94 | Assert::AreEqual(error.name(), (std::wstring)L"ReferenceError"); 95 | 96 | error = jsrt::error::create_syntax_error(L""); 97 | Assert::AreEqual(error.name(), (std::wstring)L"SyntaxError"); 98 | 99 | error = jsrt::error::create_type_error(L""); 100 | Assert::AreEqual(error.name(), (std::wstring)L"TypeError"); 101 | 102 | error = jsrt::error::create_uri_error(L""); 103 | Assert::AreEqual(error.name(), (std::wstring)L"URIError"); 104 | } 105 | runtime.dispose(); 106 | } 107 | }; 108 | } -------------------------------------------------------------------------------- /win8.1/test/jsrt-wrappers-test.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {3E7E9985-7F51-49BC-9B1F-794D27F860DB} 23 | Win32Proj 24 | jsrtwrapperstest 25 | 26 | 27 | 28 | DynamicLibrary 29 | true 30 | v120 31 | Unicode 32 | false 33 | 34 | 35 | DynamicLibrary 36 | false 37 | v120 38 | true 39 | Unicode 40 | false 41 | 42 | 43 | 44 | true 45 | 46 | 47 | 48 | Use 49 | Level3 50 | Disabled 51 | $(VCInstallDir)UnitTest\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) 52 | WIN32;_DEBUG;%(PreprocessorDefinitions) 53 | true 54 | true 55 | 56 | 57 | Windows 58 | true 59 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 60 | kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;jsrt.lib;%(AdditionalDependencies) 61 | 62 | 63 | true 64 | 65 | 66 | 67 | 68 | Level3 69 | Use 70 | MaxSpeed 71 | true 72 | true 73 | $(VCInstallDir)UnitTest\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) 74 | WIN32;NDEBUG;%(PreprocessorDefinitions) 75 | true 76 | 77 | 78 | Windows 79 | true 80 | true 81 | true 82 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 83 | jsrt.lib;%(AdditionalDependencies) 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | Create 107 | 108 | 109 | 110 | 111 | 112 | 113 | {18b6a46c-9534-498d-8463-9822bc5cac42} 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /win8.1/test/jsrt-wrappers-test.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | Source Files 64 | 65 | 66 | Source Files 67 | 68 | 69 | Source Files 70 | 71 | 72 | Source Files 73 | 74 | 75 | Source Files 76 | 77 | 78 | -------------------------------------------------------------------------------- /win8.1/test/optional.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(optional) 24 | { 25 | public: 26 | MY_TEST_METHOD(optional_values, "Test optional.") 27 | { 28 | jsrt::optional o1; 29 | Assert::IsFalse(o1.has_value()); 30 | Assert::AreEqual(o1.value(), 0); 31 | o1.clear(); 32 | o1 = 10; 33 | Assert::IsTrue(o1.has_value()); 34 | Assert::AreEqual(o1.value(), 10); 35 | o1.clear(); 36 | Assert::IsFalse(o1.has_value()); 37 | Assert::AreEqual(o1.value(), 0); 38 | o1 = jsrt::missing(); 39 | Assert::IsFalse(o1.has_value()); 40 | Assert::AreEqual(o1.value(), 0); 41 | } 42 | }; 43 | } -------------------------------------------------------------------------------- /win8.1/test/pinned.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(pinned) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_handle, "Test a pinned empty handle.") 27 | { 28 | jsrt::pinned handle; 29 | Assert::AreEqual(handle->handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(handle->is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(context_handle, "Test a pinned context handle.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::pinned context = runtime.create_context(); 37 | Assert::IsTrue(context->is_valid()); 38 | context.release(); 39 | Assert::IsFalse(context->is_valid()); 40 | runtime.dispose(); 41 | } 42 | 43 | MY_TEST_METHOD(handle_refcounting, "Test refcounting on a pinned handle.") 44 | { 45 | jsrt::runtime runtime = jsrt::runtime::create(); 46 | jsrt::context context = runtime.create_context(); 47 | { 48 | jsrt::pinned pinned_context = context; 49 | Assert::AreEqual(context.add_reference(), (unsigned int) 2); 50 | jsrt::pinned another_pinned_context = pinned_context; 51 | jsrt::pinned yet_another_pinned_context = std::move(pinned_context); 52 | Assert::IsTrue(yet_another_pinned_context->is_valid()); 53 | Assert::AreEqual(context.release(), (unsigned int) 2); 54 | } 55 | Assert::AreEqual(context.add_reference(), (unsigned int) 1); 56 | context.release(); 57 | runtime.dispose(); 58 | } 59 | }; 60 | } -------------------------------------------------------------------------------- /win8.1/test/profiler.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | class Profiler sealed : public IActiveScriptProfilerCallback2 18 | { 19 | private: 20 | long m_refCount; 21 | 22 | public: 23 | Profiler(void) 24 | { 25 | m_refCount = 1; 26 | } 27 | 28 | ~Profiler(void) 29 | { 30 | } 31 | 32 | // IUnknown 33 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObj) 34 | { 35 | if (riid == IID_IUnknown) 36 | { 37 | *ppvObj = (IUnknown *) this; 38 | } 39 | else if (riid == IID_IActiveScriptProfilerCallback) 40 | { 41 | *ppvObj = (IActiveScriptProfilerCallback *) this; 42 | } 43 | else if (riid == IID_IActiveScriptProfilerCallback2) 44 | { 45 | *ppvObj = (IActiveScriptProfilerCallback2 *) this; 46 | } 47 | else 48 | { 49 | *ppvObj = NULL; 50 | return E_NOINTERFACE; 51 | } 52 | 53 | 54 | AddRef(); 55 | return NOERROR; 56 | } 57 | 58 | ULONG STDMETHODCALLTYPE AddRef(void) 59 | { 60 | return InterlockedIncrement(&m_refCount); 61 | } 62 | 63 | ULONG STDMETHODCALLTYPE Release(void) 64 | { 65 | long lw; 66 | 67 | 68 | if (0 == (lw = InterlockedDecrement(&m_refCount))) 69 | { 70 | delete this; 71 | } 72 | return lw; 73 | } 74 | 75 | // IActiveScriptProfilerCallback 76 | HRESULT STDMETHODCALLTYPE Initialize(DWORD dwContext) { return S_OK; } 77 | HRESULT STDMETHODCALLTYPE Shutdown(HRESULT hrReason) { return S_OK; } 78 | HRESULT STDMETHODCALLTYPE ScriptCompiled(PROFILER_TOKEN scriptId, PROFILER_SCRIPT_TYPE type, IUnknown *pIDebugDocumentContext) { return S_OK; } 79 | HRESULT STDMETHODCALLTYPE FunctionCompiled(PROFILER_TOKEN functionId, PROFILER_TOKEN scriptId, const wchar_t *pwszFunctionName, const wchar_t *pwszFunctionNameHint, IUnknown *pIDebugDocumentContext) { return S_OK; } 80 | HRESULT STDMETHODCALLTYPE OnFunctionEnter(PROFILER_TOKEN scriptId, PROFILER_TOKEN functionId) { return S_OK; } 81 | HRESULT STDMETHODCALLTYPE OnFunctionExit(PROFILER_TOKEN scriptId, PROFILER_TOKEN functionId) { return S_OK; } 82 | 83 | // IActiveScriptProfilerCallback2 84 | HRESULT STDMETHODCALLTYPE OnFunctionEnterByName(const wchar_t *pwszFunctionName, PROFILER_SCRIPT_TYPE type) { return S_OK; } 85 | HRESULT STDMETHODCALLTYPE OnFunctionExitByName(const wchar_t *pwszFunctionName, PROFILER_SCRIPT_TYPE type) { return S_OK; } 86 | }; 87 | -------------------------------------------------------------------------------- /win8.1/test/property_descriptor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(property_descriptor) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_handle, "Test an empty property_descriptor handle.") 27 | { 28 | jsrt::property_descriptor handle; 29 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(handle.is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(no_context, "Test calls with no context.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::context context = runtime.create_context(); 37 | jsrt::property_descriptor property_descriptor; 38 | TEST_NO_CONTEXT_CALL(jsrt::property_descriptor::create()); 39 | TEST_NO_CONTEXT_CALL(jsrt::property_descriptor::create(jsrt::function(), jsrt::function())); 40 | TEST_NO_CONTEXT_CALL(property_descriptor.writable()); 41 | TEST_NO_CONTEXT_CALL(property_descriptor.set_writable(true)); 42 | TEST_NO_CONTEXT_CALL(property_descriptor.enumerable()); 43 | TEST_NO_CONTEXT_CALL(property_descriptor.set_enumerable(true)); 44 | TEST_NO_CONTEXT_CALL(property_descriptor.configurable()); 45 | TEST_NO_CONTEXT_CALL(property_descriptor.set_configurable(true)); 46 | TEST_NO_CONTEXT_CALL(property_descriptor.value()); 47 | TEST_NO_CONTEXT_CALL(property_descriptor.set_value(true)); 48 | TEST_NO_CONTEXT_CALL(property_descriptor.getter()); 49 | TEST_NO_CONTEXT_CALL(property_descriptor.set_getter(jsrt::function())); 50 | TEST_NO_CONTEXT_CALL(property_descriptor.setter()); 51 | TEST_NO_CONTEXT_CALL(property_descriptor.set_setter(jsrt::function())); 52 | runtime.dispose(); 53 | } 54 | 55 | static bool b(const jsrt::call_info &info) 56 | { 57 | return true; 58 | } 59 | 60 | static void set_b(const jsrt::call_info &info, bool v) 61 | { 62 | Assert::IsTrue(v); 63 | } 64 | 65 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 66 | { 67 | jsrt::runtime runtime = jsrt::runtime::create(); 68 | jsrt::context context = runtime.create_context(); 69 | { 70 | jsrt::context::scope scope(context); 71 | jsrt::property_descriptor property_descriptor; 72 | TEST_NULL_ARG_CALL(property_descriptor.writable()); 73 | TEST_NULL_ARG_CALL(property_descriptor.set_writable(true)); 74 | TEST_NULL_ARG_CALL(property_descriptor.enumerable()); 75 | TEST_NULL_ARG_CALL(property_descriptor.set_enumerable(true)); 76 | TEST_NULL_ARG_CALL(property_descriptor.configurable()); 77 | TEST_NULL_ARG_CALL(property_descriptor.set_configurable(true)); 78 | TEST_NULL_ARG_CALL(property_descriptor.value()); 79 | TEST_NULL_ARG_CALL(property_descriptor.set_value(true)); 80 | TEST_NULL_ARG_CALL(property_descriptor.getter()); 81 | TEST_NULL_ARG_CALL(property_descriptor.set_getter(jsrt::function())); 82 | TEST_NULL_ARG_CALL(property_descriptor.setter()); 83 | TEST_NULL_ARG_CALL(property_descriptor.set_setter(jsrt::function())); 84 | 85 | jsrt::property_descriptor valid_descriptor = jsrt::property_descriptor::create(); 86 | TEST_NULL_ARG_CALL(jsrt::property_descriptor::create(jsrt::function(), jsrt::function())); 87 | TEST_NULL_ARG_CALL(jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function())); 88 | TEST_NULL_ARG_CALL(property_descriptor.set_getter(jsrt::function())); 89 | TEST_NULL_ARG_CALL(property_descriptor.set_setter(jsrt::function())); 90 | } 91 | runtime.dispose(); 92 | } 93 | 94 | MY_TEST_METHOD(create, "Test ::create method.") 95 | { 96 | jsrt::runtime runtime = jsrt::runtime::create(); 97 | jsrt::context context = runtime.create_context(); 98 | { 99 | jsrt::context::scope scope(context); 100 | jsrt::value value = jsrt::property_descriptor::create(); 101 | jsrt::property_descriptor property_descriptor = (jsrt::property_descriptor)value; 102 | Assert::IsTrue(property_descriptor.is_valid()); 103 | property_descriptor = jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function::create(set_b)); 104 | Assert::IsTrue(property_descriptor.is_valid()); 105 | } 106 | runtime.dispose(); 107 | } 108 | 109 | MY_TEST_METHOD(descriptors, "Test descriptor methods.") 110 | { 111 | jsrt::runtime runtime = jsrt::runtime::create(); 112 | jsrt::context context = runtime.create_context(); 113 | { 114 | jsrt::context::scope scope(context); 115 | jsrt::object object = jsrt::object::create(); 116 | 117 | jsrt::property_descriptor desc = jsrt::property_descriptor::create(); 118 | desc.set_configurable(false); 119 | desc.set_enumerable(false); 120 | desc.set_writable(false); 121 | desc.set_value(10); 122 | object.define_property(jsrt::property_id::create(L"a"), desc); 123 | Assert::AreEqual(object.get_property(jsrt::property_id::create(L"a")), 10.0); 124 | desc = object.get_own_property_descriptor(jsrt::property_id::create(L"a")); 125 | Assert::IsFalse(desc.configurable()); 126 | Assert::IsFalse(desc.enumerable()); 127 | Assert::IsFalse(desc.writable()); 128 | Assert::AreEqual(desc.value(), 10.0); 129 | jsrt::property_descriptor bdesc = jsrt::property_descriptor::create(jsrt::function::create(b), jsrt::function::create(set_b)); 130 | object.define_property(jsrt::property_id::create(L"b"), bdesc); 131 | Assert::IsTrue(object.get_property(jsrt::property_id::create(L"b"))); 132 | object.set_property(jsrt::property_id::create(L"b"), true); 133 | } 134 | runtime.dispose(); 135 | } 136 | }; 137 | } -------------------------------------------------------------------------------- /win8.1/test/property_id.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include 17 | #include "CppUnitTest.h" 18 | 19 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 20 | 21 | namespace jsrtwrapperstest 22 | { 23 | TEST_CLASS(property_id) 24 | { 25 | public: 26 | MY_TEST_METHOD(empty_id, "Test an empty property ID.") 27 | { 28 | jsrt::property_id id; 29 | Assert::AreEqual(id.handle(), JS_INVALID_REFERENCE); 30 | Assert::IsFalse(id.is_valid()); 31 | } 32 | 33 | MY_TEST_METHOD(name, "Test a property ID.") 34 | { 35 | jsrt::runtime runtime = jsrt::runtime::create(); 36 | jsrt::context context = runtime.create_context(); 37 | { 38 | jsrt::context::scope scope(context); 39 | jsrt::property_id id = jsrt::property_id::create(L"foo"); 40 | Assert::AreEqual(id.name(), (std::wstring) L"foo"); 41 | } 42 | runtime.dispose(); 43 | } 44 | 45 | MY_TEST_METHOD(invalid, "Test property ID methods with invalid ID.") 46 | { 47 | jsrt::runtime runtime = jsrt::runtime::create(); 48 | jsrt::context context = runtime.create_context(); 49 | { 50 | jsrt::context::scope scope(context); 51 | try 52 | { 53 | jsrt::property_id id; 54 | id.name(); 55 | Assert::Fail(); 56 | } 57 | catch (jsrt::invalid_argument_exception) 58 | { 59 | } 60 | } 61 | runtime.dispose(); 62 | } 63 | 64 | MY_TEST_METHOD(no_context, "Test property ID methods with no context.") 65 | { 66 | try 67 | { 68 | jsrt::property_id::create(L"foo"); 69 | Assert::Fail(); 70 | } 71 | catch (jsrt::no_current_context_exception) 72 | { 73 | } 74 | 75 | try 76 | { 77 | jsrt::property_id foo; 78 | foo.name(); 79 | Assert::Fail(); 80 | } 81 | catch (jsrt::no_current_context_exception) 82 | { 83 | } 84 | } 85 | }; 86 | } -------------------------------------------------------------------------------- /win8.1/test/readme.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(readme) 23 | { 24 | public: 25 | static double add(const jsrt::call_info &info, double a, double b) 26 | { 27 | return a + b; 28 | } 29 | 30 | MY_TEST_METHOD(samples, "Test readme samples.") 31 | { 32 | jsrt::runtime runtime = jsrt::runtime::create(); 33 | jsrt::context context = runtime.create_context(); 34 | { 35 | jsrt::context::scope scope(context); 36 | jsrt::pinned pinned_obj = jsrt::object::create(); 37 | 38 | jsrt::object obj = jsrt::object::create(); 39 | obj.set_property(jsrt::property_id::create(L"boolProperty"), true); 40 | bool bool_value = obj.get_property(jsrt::property_id::create(L"boolProperty")); 41 | obj.set_property(jsrt::property_id::create(L"stringProperty"), L"foo"); 42 | 43 | jsrt::array darray = jsrt::array::create(1); 44 | darray[0] = 10; 45 | darray[1] = 20; 46 | 47 | auto f = (jsrt::function)jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;"); 48 | double a = f(jsrt::context::undefined(), 1, 2); 49 | 50 | auto nf = jsrt::function::create(add); 51 | jsrt::context::global().set_property(jsrt::property_id::create(L"add"), nf); 52 | jsrt::context::run(L"add(1, 2)"); 53 | 54 | auto bf = jsrt::bound_function( 55 | jsrt::context::undefined(), 56 | (jsrt::function)jsrt::context::evaluate(L"function f(a, b) { return a + b; }; f;")); 57 | double ba = bf(1, 2); 58 | } 59 | runtime.dispose(); 60 | } 61 | }; 62 | } -------------------------------------------------------------------------------- /win8.1/test/reference.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(reference) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty handle.") 26 | { 27 | jsrt::reference handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | } 31 | 32 | MY_TEST_METHOD(context_handle, "Test a context handle.") 33 | { 34 | jsrt::runtime runtime = jsrt::runtime::create(); 35 | jsrt::context context = runtime.create_context(); 36 | Assert::IsTrue(context.is_valid()); 37 | runtime.dispose(); 38 | } 39 | 40 | MY_TEST_METHOD(handle_refcounting, "Test refcounting on a handle.") 41 | { 42 | jsrt::runtime runtime = jsrt::runtime::create(); 43 | jsrt::context context = runtime.create_context(); 44 | Assert::AreEqual(context.add_reference(), (unsigned int) 1); 45 | Assert::AreEqual(context.release(), (unsigned int) 0); 46 | runtime.dispose(); 47 | } 48 | 49 | MY_TEST_METHOD(invalid_handle, "Test refcounting on an invalid handle.") 50 | { 51 | jsrt::context context; 52 | 53 | try 54 | { 55 | context.add_reference(); 56 | Assert::Fail(); 57 | } 58 | catch (jsrt::null_argument_exception) 59 | { 60 | } 61 | 62 | try 63 | { 64 | context.release(); 65 | Assert::Fail(); 66 | } 67 | catch (jsrt::null_argument_exception) 68 | { 69 | } 70 | } 71 | }; 72 | } -------------------------------------------------------------------------------- /win8.1/test/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | -------------------------------------------------------------------------------- /win8.1/test/stdafx.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "targetver.h" 18 | #define WIN32_LEAN_AND_MEAN 19 | #include "jsrt.h" 20 | #include "jsrt-wrappers.h" 21 | #include "profiler.h" 22 | #include "CppUnitTest.h" 23 | 24 | #define MY_TEST_METHOD(name, description) \ 25 | BEGIN_TEST_METHOD_ATTRIBUTE(name) \ 26 | TEST_DESCRIPTION(description) \ 27 | END_TEST_METHOD_ATTRIBUTE() \ 28 | \ 29 | TEST_METHOD(name) 30 | 31 | #define MY_TEST_METHOD_DISABLED(name, description) \ 32 | BEGIN_TEST_METHOD_ATTRIBUTE(name) \ 33 | TEST_DESCRIPTION(description) \ 34 | TEST_IGNORE() \ 35 | END_TEST_METHOD_ATTRIBUTE() \ 36 | \ 37 | TEST_METHOD(name) 38 | 39 | #define TEST_FAILED_CALL(call, exception) \ 40 | try \ 41 | { \ 42 | call; \ 43 | Assert::Fail(); \ 44 | } \ 45 | catch (const jsrt:: exception &) \ 46 | { \ 47 | } 48 | 49 | #define TEST_INVALID_ARG_CALL(call) \ 50 | TEST_FAILED_CALL(call, invalid_argument_exception) 51 | 52 | #define TEST_NO_CONTEXT_CALL(call) \ 53 | TEST_FAILED_CALL(call, no_current_context_exception) 54 | 55 | #define TEST_NULL_ARG_CALL(call) \ 56 | TEST_FAILED_CALL(call, null_argument_exception) 57 | 58 | #define TEST_SCRIPT_EXCEPTION_CALL(call) \ 59 | TEST_FAILED_CALL(call, script_exception) 60 | 61 | #define IfComFailError(v) \ 62 | { \ 63 | hr = (v); \ 64 | if (FAILED(hr)) \ 65 | { \ 66 | goto error; \ 67 | } \ 68 | } 69 | 70 | extern IDebugApplication *get_debug_application(); 71 | 72 | template <> 73 | static std::wstring Microsoft::VisualStudio::CppUnitTestFramework::ToString(const JsValueType& q) 74 | { 75 | switch (q) 76 | { 77 | case JsUndefined: 78 | return L"JsUndefined"; 79 | case JsNull: 80 | return L"JsNull"; 81 | case JsNumber: 82 | return L"JsNumber"; 83 | case JsString: 84 | return L"JsString"; 85 | case JsBoolean: 86 | return L"JsBoolean"; 87 | case JsObject: 88 | return L"JsObject"; 89 | case JsFunction: 90 | return L"JsFunction"; 91 | case JsError: 92 | return L"JsError"; 93 | case JsArray: 94 | return L"JsArray"; 95 | default: 96 | return std::wstring(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /win8.1/test/targetver.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | -------------------------------------------------------------------------------- /win8.1/test/value.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Paul Vick 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "stdafx.h" 16 | #include "CppUnitTest.h" 17 | 18 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 19 | 20 | namespace jsrtwrapperstest 21 | { 22 | TEST_CLASS(value) 23 | { 24 | public: 25 | MY_TEST_METHOD(empty_handle, "Test an empty value handle.") 26 | { 27 | jsrt::value handle; 28 | Assert::AreEqual(handle.handle(), JS_INVALID_REFERENCE); 29 | Assert::IsFalse(handle.is_valid()); 30 | 31 | jsrt::boolean boolean_handle; 32 | Assert::AreEqual(boolean_handle.handle(), JS_INVALID_REFERENCE); 33 | Assert::IsFalse(boolean_handle.is_valid()); 34 | 35 | jsrt::number number_handle; 36 | Assert::AreEqual(number_handle.handle(), JS_INVALID_REFERENCE); 37 | Assert::IsFalse(number_handle.is_valid()); 38 | 39 | jsrt::string string_handle; 40 | Assert::AreEqual(string_handle.handle(), JS_INVALID_REFERENCE); 41 | Assert::IsFalse(string_handle.is_valid()); 42 | } 43 | 44 | MY_TEST_METHOD(no_context, "Test calls with no context.") 45 | { 46 | jsrt::runtime runtime = jsrt::runtime::create(); 47 | jsrt::context context = runtime.create_context(); 48 | jsrt::value value; 49 | VARIANT v; 50 | TEST_NO_CONTEXT_CALL(value.type()); 51 | TEST_NO_CONTEXT_CALL(value.to_variant(&v)); 52 | TEST_NO_CONTEXT_CALL(jsrt::value::from_variant(&v)); 53 | TEST_NO_CONTEXT_CALL(jsrt::boolean::create(true)); 54 | TEST_NO_CONTEXT_CALL(jsrt::boolean::true_value()); 55 | TEST_NO_CONTEXT_CALL(jsrt::boolean::false_value()); 56 | TEST_NO_CONTEXT_CALL(((jsrt::boolean)value).data()); 57 | TEST_NO_CONTEXT_CALL(jsrt::number::create(1.0)); 58 | TEST_NO_CONTEXT_CALL(((jsrt::number)value).data()); 59 | TEST_NO_CONTEXT_CALL(jsrt::string::create(L"foo")); 60 | TEST_NO_CONTEXT_CALL(((jsrt::string)value).data()); 61 | TEST_NO_CONTEXT_CALL(((jsrt::string)value).length()); 62 | runtime.dispose(); 63 | } 64 | 65 | MY_TEST_METHOD(invalid_handle, "Test calls with an invalid handle.") 66 | { 67 | jsrt::runtime runtime = jsrt::runtime::create(); 68 | jsrt::context context = runtime.create_context(); 69 | { 70 | jsrt::context::scope scope(context); 71 | jsrt::value value; 72 | VARIANT v; 73 | TEST_NULL_ARG_CALL(value.type()); 74 | TEST_NULL_ARG_CALL(value.to_variant(&v)); 75 | jsrt::boolean boolean; 76 | TEST_NULL_ARG_CALL(boolean.data()); 77 | jsrt::number number; 78 | TEST_NULL_ARG_CALL(number.data()); 79 | jsrt::string string; 80 | TEST_NULL_ARG_CALL(string.data()); 81 | TEST_NULL_ARG_CALL(string.length()); 82 | } 83 | runtime.dispose(); 84 | } 85 | 86 | MY_TEST_METHOD(type, "Test the ::type method.") 87 | { 88 | jsrt::runtime runtime = jsrt::runtime::create(); 89 | jsrt::context context = runtime.create_context(); 90 | { 91 | jsrt::context::scope scope(context); 92 | jsrt::value value; 93 | value = jsrt::context::undefined(); 94 | Assert::AreEqual(value.type(), JsUndefined); 95 | value = jsrt::context::null(); 96 | Assert::AreEqual(value.type(), JsNull); 97 | value = jsrt::boolean::true_value(); 98 | Assert::AreEqual(value.type(), JsBoolean); 99 | value = jsrt::number::create(10); 100 | Assert::AreEqual(value.type(), JsNumber); 101 | value = jsrt::string::create(L"foo"); 102 | Assert::AreEqual(value.type(), JsString); 103 | value = jsrt::object::create(); 104 | Assert::AreEqual(value.type(), JsObject); 105 | value = jsrt::context::parse(L"1 + 2;"); 106 | Assert::AreEqual(value.type(), JsFunction); 107 | value = jsrt::error::create_uri_error(L"foo"); 108 | Assert::AreEqual(value.type(), JsError); 109 | value = jsrt::array::create(10); 110 | Assert::AreEqual(value.type(), JsArray); 111 | } 112 | runtime.dispose(); 113 | } 114 | 115 | MY_TEST_METHOD(variant, "Test the ::to_variant and ::from_variant methods.") 116 | { 117 | jsrt::runtime runtime = jsrt::runtime::create(); 118 | jsrt::context context = runtime.create_context(); 119 | { 120 | jsrt::context::scope scope(context); 121 | VARIANT v; 122 | v.vt = VT_I4; 123 | v.intVal = 42; 124 | jsrt::value value = jsrt::value::from_variant(&v); 125 | Assert::AreEqual((int) value.type(), (int) JsNumber); 126 | Assert::AreEqual(((jsrt::number) value).data(), 42.0); 127 | value = jsrt::number::create(32); 128 | value.to_variant(&v); 129 | Assert::AreEqual((int) v.vt, (int) VT_I4); 130 | Assert::AreEqual(v.intVal, 32); 131 | } 132 | runtime.dispose(); 133 | } 134 | 135 | MY_TEST_METHOD(equals, "Test the ::equals and ::strict_equals methods.") 136 | { 137 | jsrt::runtime runtime = jsrt::runtime::create(); 138 | jsrt::context context = runtime.create_context(); 139 | { 140 | jsrt::context::scope scope(context); 141 | jsrt::value v1 = jsrt::number::create(1); 142 | jsrt::value v2 = jsrt::string::create(L"1"); 143 | jsrt::value v3 = jsrt::number::create(1); 144 | Assert::IsTrue(v1.equals(v2)); 145 | Assert::IsTrue(v1.equals(v3)); 146 | Assert::IsFalse(v1.strict_equals(v2)); 147 | Assert::IsTrue(v1.strict_equals(v3)); 148 | } 149 | runtime.dispose(); 150 | } 151 | 152 | MY_TEST_METHOD(boolean, "Test boolean methods.") 153 | { 154 | jsrt::runtime runtime = jsrt::runtime::create(); 155 | jsrt::context context = runtime.create_context(); 156 | { 157 | jsrt::context::scope scope(context); 158 | jsrt::value value = jsrt::boolean::create(true); 159 | jsrt::boolean boolean = jsrt::boolean::convert(value); 160 | Assert::AreEqual(value.type(), JsBoolean); 161 | Assert::IsTrue(boolean.data()); 162 | Assert::IsTrue(jsrt::boolean::true_value().data()); 163 | Assert::IsFalse(jsrt::boolean::false_value().data()); 164 | } 165 | runtime.dispose(); 166 | } 167 | 168 | MY_TEST_METHOD(number, "Test number methods.") 169 | { 170 | jsrt::runtime runtime = jsrt::runtime::create(); 171 | jsrt::context context = runtime.create_context(); 172 | { 173 | jsrt::context::scope scope(context); 174 | jsrt::value value = jsrt::number::create(1.0); 175 | jsrt::number number = jsrt::number::convert(value); 176 | Assert::AreEqual(value.type(), JsNumber); 177 | Assert::AreEqual(number.data(), 1.0); 178 | number = jsrt::number::create(42); 179 | Assert::AreEqual(number.data(), 42.0); 180 | } 181 | runtime.dispose(); 182 | } 183 | 184 | MY_TEST_METHOD(string, "Test string methods.") 185 | { 186 | jsrt::runtime runtime = jsrt::runtime::create(); 187 | jsrt::context context = runtime.create_context(); 188 | { 189 | jsrt::context::scope scope(context); 190 | jsrt::value value = jsrt::string::create(L"foo"); 191 | jsrt::string string = jsrt::string::convert(value); 192 | Assert::AreEqual(value.type(), JsString); 193 | Assert::AreEqual(string.data(), (std::wstring) L"foo"); 194 | Assert::AreEqual(string.length(), 3); 195 | } 196 | runtime.dispose(); 197 | } 198 | }; 199 | } --------------------------------------------------------------------------------