Skip to content

Commit b763dd6

Browse files
committed
Add C++98 native test to ensure we don't break
1 parent 247f6b9 commit b763dd6

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
var isWindows = !WScript.Platform || WScript.Platform.OS == 'win32';
7+
var path_sep = isWindows ? '\\' : '/';
8+
var isStaticBuild = WScript.Platform && WScript.Platform.LINK_TYPE == 'static';
9+
10+
if (!isStaticBuild) {
11+
// test will be ignored
12+
print("# IGNORE_THIS_TEST");
13+
} else {
14+
var platform = WScript.Platform.OS;
15+
var binaryPath = WScript.Platform.BINARY_PATH;
16+
// discard `ch` from path
17+
binaryPath = binaryPath.substr(0, binaryPath.lastIndexOf(path_sep));
18+
var makefile =
19+
"IDIR=" + binaryPath + "/../../lib/Jsrt \n\
20+
\n\
21+
LIBRARY_PATH=" + binaryPath + "/lib\n\
22+
PLATFORM=" + platform + "\n\
23+
LDIR=$(LIBRARY_PATH)/../pal/src/libChakra.Pal.a \
24+
$(LIBRARY_PATH)/Common/Core/libChakra.Common.Core.a \
25+
$(LIBRARY_PATH)/Jsrt/libChakra.Jsrt.a \n\
26+
\n\
27+
ifeq (darwin, ${PLATFORM})\n\
28+
\tICU4C_LIBRARY_PATH ?= /usr/local/opt/icu4c\n\
29+
\tCFLAGS=-lstdc++ -std=c++98 -I$(IDIR)\n\
30+
\tFORCE_STARTS=-Wl,-force_load,\n\
31+
\tFORCE_ENDS=\n\
32+
\tLIBS=-framework CoreFoundation -framework Security -lm -ldl -Wno-c++11-compat-deprecated-writable-strings \
33+
-Wno-deprecated-declarations -Wno-unknown-warning-option -o sample.o\n\
34+
\tLDIR+=$(ICU4C_LIBRARY_PATH)/lib/libicudata.a \
35+
$(ICU4C_LIBRARY_PATH)/lib/libicuuc.a \
36+
$(ICU4C_LIBRARY_PATH)/lib/libicui18n.a\n\
37+
else\n\
38+
\tCFLAGS=-lstdc++ -std=c++0x -I$(IDIR)\n\
39+
\tFORCE_STARTS=-Wl,--whole-archive\n\
40+
\tFORCE_ENDS=-Wl,--no-whole-archive\n\
41+
\tLIBS=-pthread -lm -ldl -licuuc -lunwind-x86_64 -Wno-c++11-compat-deprecated-writable-strings \
42+
-Wno-deprecated-declarations -Wno-unknown-warning-option -o sample.o\n\
43+
endif\n\
44+
\n\
45+
testmake:\n\
46+
\t$(CC) sample.cpp $(CFLAGS) $(FORCE_STARTS) $(LDIR) $(FORCE_ENDS) $(LIBS)\n\
47+
\n\
48+
.PHONY: clean\n\
49+
\n\
50+
clean:\n\
51+
\trm sample.o\n";
52+
53+
print(makefile)
54+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
#include "ChakraCore.h"
7+
#include <stdlib.h>
8+
#include <stdio.h>
9+
#include <string>
10+
#include <cstring>
11+
12+
#define FAIL_CHECK(cmd) \
13+
do \
14+
{ \
15+
JsErrorCode errCode = cmd; \
16+
if (errCode != JsNoError) \
17+
{ \
18+
printf("Error %d at '%s'\n", \
19+
errCode, #cmd); \
20+
return 1; \
21+
} \
22+
} while(0)
23+
24+
using namespace std;
25+
26+
int main()
27+
{
28+
JsRuntimeHandle runtime;
29+
JsContextRef context;
30+
JsValueRef result;
31+
unsigned currentSourceContext = 0;
32+
33+
const char* script = "(()=>{return \'SUCCESS\';})()";
34+
size_t length = strlen(script);
35+
36+
// Create a runtime.
37+
JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);
38+
39+
// Create an execution context.
40+
JsCreateContext(runtime, &context);
41+
42+
// Now set the current execution context.
43+
JsSetCurrentContext(context);
44+
45+
JsValueRef fname;
46+
FAIL_CHECK(JsCreateString("sample", strlen("sample"), &fname));
47+
48+
JsValueRef scriptSource;
49+
FAIL_CHECK(JsCreateString(script, length, &scriptSource));
50+
51+
// Run the script.
52+
FAIL_CHECK(JsRun(scriptSource, currentSourceContext++, fname,
53+
JsParseScriptAttributeNone, &result));
54+
55+
// Convert your script result to String in JavaScript; redundant if your script returns a String
56+
JsValueRef resultJSString;
57+
FAIL_CHECK(JsConvertValueToString(result, &resultJSString));
58+
59+
// Project script result back to C++.
60+
uint8_t *resultSTR = nullptr;
61+
size_t stringLength;
62+
FAIL_CHECK(JsCopyStringUtf8(resultJSString, nullptr, 0, &stringLength));
63+
resultSTR = (uint8_t*)malloc(stringLength + 1);
64+
FAIL_CHECK(JsCopyStringUtf8(resultJSString, resultSTR, stringLength + 1, nullptr));
65+
resultSTR[stringLength] = 0;
66+
67+
printf("Result -> %s \n", resultSTR);
68+
free(resultSTR);
69+
70+
// Dispose runtime
71+
JsSetCurrentContext(JS_INVALID_REFERENCE);
72+
JsDisposeRuntime(runtime);
73+
74+
return 0;
75+
}

test/native-tests/test_native.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ RUN () {
6565
fi
6666
}
6767

68+
# test-c98
69+
RUN "test-c98"
70+
71+
# test-char
72+
RUN "test-char"
73+
6874
# test-char16
6975
RUN "test-char16"
7076

0 commit comments

Comments
 (0)