Skip to content

Commit 75cfe99

Browse files
committed
Merged PR 153810: Provide JSRT APIs to get Array prototype iteration functions. Also, fixes a few bing.com bugs.
Provide JSRT APIs to get Array prototype iteration functions. Also, fixes a few bing.com bugs.
2 parents 36ae40f + 469b086 commit 75cfe99

File tree

6 files changed

+206
-7
lines changed

6 files changed

+206
-7
lines changed

bin/ch/ChakraRtInterface.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ bool ChakraRTInterface::LoadChakraDll(ArgInfo* argInfo, HINSTANCE *outLibrary)
8686
m_jsApiHooks.pfJsrtCreateObject = (JsAPIHooks::JsrtCreateObjectPtr)GetChakraCoreSymbol(library, "JsCreateObject");
8787
m_jsApiHooks.pfJsrtCreateExternalObject = (JsAPIHooks::JsrtCreateExternalObjectPtr)GetChakraCoreSymbol(library, "JsCreateExternalObject");
8888
m_jsApiHooks.pfJsrtCreateCustomExternalObject = (JsAPIHooks::JsrtCreateCustomExternalObjectPtr)GetChakraCoreSymbol(library, "JsCreateCustomExternalObject");
89+
m_jsApiHooks.pfJsrtGetArrayForEachFunction = (JsAPIHooks::JsrtGetArrayForEachFunctionPtr)GetChakraCoreSymbol(library, "JsGetArrayForEachFunction");;
90+
m_jsApiHooks.pfJsrtGetArrayKeysFunction = (JsAPIHooks::JsrtGetArrayKeysFunctionPtr)GetChakraCoreSymbol(library, "JsGetArrayKeysFunction");;
91+
m_jsApiHooks.pfJsrtGetArrayValuesFunction = (JsAPIHooks::JsrtGetArrayValuesFunctionPtr)GetChakraCoreSymbol(library, "JsGetArrayValuesFunction");;
92+
m_jsApiHooks.pfJsrtGetArrayEntriesFunction = (JsAPIHooks::JsrtGetArrayEntriesFunctionPtr)GetChakraCoreSymbol(library, "JsGetArrayEntriesFunction");;
93+
m_jsApiHooks.pfJsrtGetPropertyIdSymbolIterator = (JsAPIHooks::JsrtGetPropertyIdSymbolIteratorPtr)GetChakraCoreSymbol(library, "JsGetPropertyIdSymbolIterator");;
94+
m_jsApiHooks.pfJsrtGetErrorPrototype = (JsAPIHooks::JsrtGetErrorPrototypePtr)GetChakraCoreSymbol(library, "JsGetErrorPrototype");;
95+
m_jsApiHooks.pfJsrtGetIteratorPrototype = (JsAPIHooks::JsrtGetIteratorPrototypePtr)GetChakraCoreSymbol(library, "JsGetIteratorPrototype");;
8996
m_jsApiHooks.pfJsrtCreateFunction = (JsAPIHooks::JsrtCreateFunctionPtr)GetChakraCoreSymbol(library, "JsCreateFunction");
9097
m_jsApiHooks.pfJsrtCreateNamedFunction = (JsAPIHooks::JsCreateNamedFunctionPtr)GetChakraCoreSymbol(library, "JsCreateNamedFunction");
9198
m_jsApiHooks.pfJsrtSetProperty = (JsAPIHooks::JsrtSetPropertyPtr)GetChakraCoreSymbol(library, "JsSetProperty");

bin/ch/ChakraRtInterface.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ struct JsAPIHooks
1717
typedef JsErrorCode (WINAPI *JsrtCreateObjectPtr)(JsValueRef *object);
1818
typedef JsErrorCode (WINAPI *JsrtCreateExternalObjectPtr)(void* data, JsFinalizeCallback callback, JsValueRef *object);
1919
typedef JsErrorCode (WINAPI *JsrtCreateCustomExternalObjectPtr)(void* data, JsFinalizeCallback callback, JsValueRef getterSetterInterceptor, JsValueRef *object);
20+
typedef JsErrorCode (WINAPI *JsrtGetArrayForEachFunctionPtr)(JsValueRef *result);
21+
typedef JsErrorCode (WINAPI *JsrtGetArrayKeysFunctionPtr)(JsValueRef *result);
22+
typedef JsErrorCode (WINAPI *JsrtGetArrayValuesFunctionPtr)(JsValueRef *result);
23+
typedef JsErrorCode (WINAPI *JsrtGetArrayEntriesFunctionPtr)(JsValueRef *result);
24+
typedef JsErrorCode (WINAPI *JsrtGetPropertyIdSymbolIteratorPtr)(JsPropertyIdRef *propertyId);
25+
typedef JsErrorCode (WINAPI *JsrtGetErrorPrototypePtr)(JsValueRef *result);
26+
typedef JsErrorCode (WINAPI *JsrtGetIteratorPrototypePtr)(JsValueRef *result);
2027
typedef JsErrorCode (WINAPI *JsrtCreateFunctionPtr)(JsNativeFunction nativeFunction, void *callbackState, JsValueRef *function);
2128
typedef JsErrorCode (WINAPI *JsrtCreateEnhancedFunctionPtr)(JsEnhancedNativeFunction nativeFunction, JsValueRef metadata, void *callbackState, JsValueRef *function);
2229
typedef JsErrorCode (WINAPI *JsCreateNamedFunctionPtr)(JsValueRef name, JsNativeFunction nativeFunction, void *callbackState, JsValueRef *function);
@@ -127,6 +134,14 @@ struct JsAPIHooks
127134
JsrtCreateObjectPtr pfJsrtCreateObject;
128135
JsrtCreateExternalObjectPtr pfJsrtCreateExternalObject;
129136
JsrtCreateCustomExternalObjectPtr pfJsrtCreateCustomExternalObject;
137+
JsrtGetArrayForEachFunctionPtr pfJsrtGetArrayForEachFunction;
138+
JsrtGetArrayKeysFunctionPtr pfJsrtGetArrayKeysFunction;
139+
JsrtGetArrayValuesFunctionPtr pfJsrtGetArrayValuesFunction;
140+
JsrtGetArrayEntriesFunctionPtr pfJsrtGetArrayEntriesFunction;
141+
JsrtGetPropertyIdSymbolIteratorPtr pfJsrtGetPropertyIdSymbolIterator;
142+
JsrtGetErrorPrototypePtr pfJsrtGetErrorPrototype;
143+
JsrtGetIteratorPrototypePtr pfJsrtGetIteratorPrototype;
144+
130145
JsrtCreateFunctionPtr pfJsrtCreateFunction;
131146
JsrtCreateEnhancedFunctionPtr pfJsrtCreateEnhancedFunction;
132147
JsCreateNamedFunctionPtr pfJsrtCreateNamedFunction;
@@ -351,6 +366,13 @@ class ChakraRTInterface
351366
static JsErrorCode WINAPI JsCreateObject(JsValueRef *object) { return HOOK_JS_API(CreateObject(object)); }
352367
static JsErrorCode WINAPI JsCreateExternalObject(void *data, JsFinalizeCallback callback, JsValueRef *object) { return HOOK_JS_API(CreateExternalObject(data, callback, object)); }
353368
static JsErrorCode WINAPI JsCreateCustomExternalObject(void *data, JsFinalizeCallback callback, JsValueRef getterSetterInterceptor, JsValueRef * object) { return HOOK_JS_API(CreateCustomExternalObject(data, callback, getterSetterInterceptor, object)); }
369+
static JsErrorCode WINAPI JsGetArrayForEachFunction(JsValueRef * result) { return HOOK_JS_API(GetArrayForEachFunction(result)); }
370+
static JsErrorCode WINAPI JsGetArrayKeysFunction(JsValueRef * result) { return HOOK_JS_API(GetArrayKeysFunction(result)); }
371+
static JsErrorCode WINAPI JsGetArrayValuesFunction(JsValueRef * result) { return HOOK_JS_API(GetArrayValuesFunction(result)); }
372+
static JsErrorCode WINAPI JsGetArrayEntriesFunction(JsValueRef * result) { return HOOK_JS_API(GetArrayEntriesFunction(result)); }
373+
static JsErrorCode WINAPI JsGetPropertyIdSymbolIterator(JsPropertyIdRef * propertyId) { return HOOK_JS_API(GetPropertyIdSymbolIterator(propertyId)); }
374+
static JsErrorCode WINAPI JsGetErrorPrototype(JsValueRef * result) { return HOOK_JS_API(GetErrorPrototype(result)); }
375+
static JsErrorCode WINAPI JsGetIteratorPrototype(JsValueRef * result) { return HOOK_JS_API(GetIteratorPrototype(result)); }
354376
static JsErrorCode WINAPI JsCreateFunction(JsNativeFunction nativeFunction, void *callbackState, JsValueRef *function) { return HOOK_JS_API(CreateFunction(nativeFunction, callbackState, function)); }
355377
static JsErrorCode WINAPI JsCreateEnhancedFunction(JsEnhancedNativeFunction nativeFunction, JsValueRef metadata, void *callbackState, JsValueRef *function) { return HOOK_JS_API(CreateEnhancedFunction(nativeFunction, metadata, callbackState, function)); }
356378
static JsErrorCode WINAPI JsCreateNamedFunction(JsValueRef name, JsNativeFunction nativeFunction, void *callbackState, JsValueRef *function) { return HOOK_JS_API(CreateNamedFunction(name, nativeFunction, callbackState, function)); }

lib/Jsrt/ChakraCore.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,55 @@ JsCreateTracedCustomExternalObjectWithPrototype(
11471147
_In_opt_ JsValueRef prototype,
11481148
_Out_ JsValueRef * object);
11491149

1150+
/// <summary>
1151+
/// Returns a reference to the Array.Prototype.forEach function. The function is created if it's not already present.
1152+
/// </summary>
1153+
/// <param name="result">A reference to the Array.Prototype.forEach function.</param>
1154+
CHAKRA_API
1155+
JsGetArrayForEachFunction(_Out_ JsValueRef * result);
1156+
1157+
/// <summary>
1158+
/// Returns a reference to the Array.Prototype.keys function. The function is created if it's not already present.
1159+
/// </summary>
1160+
/// <param name="result">A reference to the Array.Prototype.keys function.</param>
1161+
CHAKRA_API
1162+
JsGetArrayKeysFunction(_Out_ JsValueRef * result);
1163+
1164+
/// <summary>
1165+
/// Returns a reference to the Array.Prototype.values function. The function is created if it's not already present.
1166+
/// </summary>
1167+
/// <param name="result">A reference to the Array.Prototype.values function.</param>
1168+
CHAKRA_API
1169+
JsGetArrayValuesFunction(_Out_ JsValueRef * result);
1170+
1171+
/// <summary>
1172+
/// Returns a reference to the Array.Prototype.entries function. The function is created if it's not already present.
1173+
/// </summary>
1174+
/// <param name="result">A reference to the Array.Prototype.entries function.</param>
1175+
CHAKRA_API
1176+
JsGetArrayEntriesFunction(_Out_ JsValueRef * result);
1177+
1178+
/// <summary>
1179+
/// Returns the property id of the Symbol.iterator property.
1180+
/// </summary>
1181+
/// <param name="propertyId">The property id of the Symbol.iterator property.</param>
1182+
CHAKRA_API
1183+
JsGetPropertyIdSymbolIterator(_Out_ JsPropertyIdRef * propertyId);
1184+
1185+
/// <summary>
1186+
/// Returns a reference to the Javascript error prototype object.
1187+
/// </summary>
1188+
/// <param name="result">A reference to the Javascript error prototype object.</param>
1189+
CHAKRA_API
1190+
JsGetErrorPrototype(_Out_ JsValueRef * result);
1191+
1192+
/// <summary>
1193+
/// Returns a reference to the Javascript iterator prototype object.
1194+
/// </summary>
1195+
/// <param name="result">A reference to the Javascript iterator prototype object.</param>
1196+
CHAKRA_API
1197+
JsGetIteratorPrototype(_Out_ JsValueRef * result);
1198+
11501199
/// <summary>
11511200
/// Creates a new object (with prototype) that stores some external data and also supports interceptors.
11521201
/// </summary>
@@ -1574,7 +1623,7 @@ JsSetRuntimeDomWrapperTracingCallbacks(
15741623
_In_ JsDOMWrapperTracingEnterFinalPauseCallback enterFinalPauseCallback);
15751624

15761625
CHAKRA_API
1577-
JsTraceExternalReference(
1626+
JsTraceExternalReference(
15781627
_In_ JsRuntimeHandle runtimeHandle,
15791628
_In_ JsValueRef value
15801629
);

lib/Jsrt/Jsrt.cpp

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,10 +1824,17 @@ CHAKRA_API JsHasProperty(_In_ JsValueRef object, _In_ JsPropertyIdRef propertyId
18241824
{
18251825
return ContextAPIWrapper<JSRT_MAYBE_TRUE>(internalHasProperty);
18261826
}
1827-
else
1827+
else if (typeId == Js::TypeIds_Object)
18281828
{
1829-
return ContextAPINoScriptWrapper(internalHasProperty);
1829+
// CEOs can also have traps so we would want the Enter/Leave semantics for those.
1830+
Js::CustomExternalWrapperObject * externalWrapper = Js::JavascriptOperators::TryFromVar<Js::CustomExternalWrapperObject>(object);
1831+
if (externalWrapper)
1832+
{
1833+
return ContextAPIWrapper<JSRT_MAYBE_TRUE>(internalHasProperty);
1834+
}
18301835
}
1836+
1837+
return ContextAPINoScriptWrapper(internalHasProperty);
18311838
}
18321839

18331840
#ifdef _CHAKRACOREBUILD
@@ -1870,10 +1877,17 @@ CHAKRA_API JsObjectHasProperty(_In_ JsValueRef object, _In_ JsValueRef propertyI
18701877
{
18711878
return ContextAPIWrapper<JSRT_MAYBE_TRUE>(internalHasProperty);
18721879
}
1873-
else
1880+
else if (typeId == Js::TypeIds_Object)
18741881
{
1875-
return ContextAPINoScriptWrapper(internalHasProperty);
1882+
// CEOs can also have traps so we would want the Enter/Leave semantics for those.
1883+
Js::CustomExternalWrapperObject * externalWrapper = Js::JavascriptOperators::TryFromVar<Js::CustomExternalWrapperObject>(object);
1884+
if (externalWrapper)
1885+
{
1886+
return ContextAPIWrapper<JSRT_MAYBE_TRUE>(internalHasProperty);
1887+
}
18761888
}
1889+
1890+
return ContextAPINoScriptWrapper(internalHasProperty);
18771891
}
18781892
#endif
18791893

@@ -6166,6 +6180,106 @@ JsSetRuntimeDomWrapperTracingCallbacks(
61666180
});
61676181
}
61686182

6183+
CHAKRA_API
6184+
JsGetArrayForEachFunction(_Out_ JsValueRef * result)
6185+
{
6186+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext *scriptContext) -> JsErrorCode {
6187+
PARAM_NOT_NULL(result);
6188+
6189+
*result = scriptContext->GetLibrary()->EnsureArrayPrototypeForEachFunction();
6190+
6191+
return JsNoError;
6192+
},
6193+
/*allowInObjectBeforeCollectCallback*/true);
6194+
}
6195+
6196+
CHAKRA_API
6197+
JsGetArrayKeysFunction(_Out_ JsValueRef * result)
6198+
{
6199+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext *scriptContext) -> JsErrorCode {
6200+
PARAM_NOT_NULL(result);
6201+
6202+
*result = scriptContext->GetLibrary()->EnsureArrayPrototypeKeysFunction();
6203+
6204+
return JsNoError;
6205+
},
6206+
/*allowInObjectBeforeCollectCallback*/true);
6207+
}
6208+
6209+
CHAKRA_API
6210+
JsGetArrayValuesFunction(_Out_ JsValueRef * result)
6211+
{
6212+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext *scriptContext) -> JsErrorCode {
6213+
PARAM_NOT_NULL(result);
6214+
6215+
*result = scriptContext->GetLibrary()->EnsureArrayPrototypeValuesFunction();
6216+
6217+
return JsNoError;
6218+
},
6219+
/*allowInObjectBeforeCollectCallback*/true);
6220+
}
6221+
6222+
CHAKRA_API
6223+
JsGetArrayEntriesFunction(_Out_ JsValueRef * result)
6224+
{
6225+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext *scriptContext) -> JsErrorCode {
6226+
PARAM_NOT_NULL(result);
6227+
6228+
*result = scriptContext->GetLibrary()->EnsureArrayPrototypeEntriesFunction();
6229+
6230+
return JsNoError;
6231+
},
6232+
/*allowInObjectBeforeCollectCallback*/true);
6233+
}
6234+
6235+
CHAKRA_API
6236+
JsGetPropertyIdSymbolIterator(_Out_ JsPropertyIdRef * propertyId)
6237+
{
6238+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext *scriptContext) -> JsErrorCode {
6239+
PARAM_NOT_NULL(propertyId);
6240+
6241+
Js::PropertyId symbolIteratorPropertyId = scriptContext->GetLibrary()->GetPropertyIdSymbolIterator();
6242+
*propertyId = Js::JavascriptNumber::ToVar(symbolIteratorPropertyId, scriptContext);
6243+
6244+
return JsNoError;
6245+
},
6246+
/*allowInObjectBeforeCollectCallback*/true);
6247+
}
6248+
6249+
CHAKRA_API
6250+
JsGetErrorPrototype(_Out_ JsValueRef * result)
6251+
{
6252+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext *scriptContext) -> JsErrorCode {
6253+
PARAM_NOT_NULL(result);
6254+
6255+
*result = scriptContext->GetLibrary()->GetErrorPrototype();
6256+
if (*result == JS_INVALID_REFERENCE)
6257+
{
6258+
return JsErrorFatal;
6259+
}
6260+
6261+
return JsNoError;
6262+
},
6263+
/*allowInObjectBeforeCollectCallback*/true);
6264+
}
6265+
6266+
CHAKRA_API
6267+
JsGetIteratorPrototype(_Out_ JsValueRef * result)
6268+
{
6269+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext *scriptContext) -> JsErrorCode {
6270+
PARAM_NOT_NULL(result);
6271+
6272+
*result = scriptContext->GetLibrary()->GetIteratorPrototype();
6273+
if (*result == JS_INVALID_REFERENCE)
6274+
{
6275+
return JsErrorFatal;
6276+
}
6277+
6278+
return JsNoError;
6279+
},
6280+
/*allowInObjectBeforeCollectCallback*/true);
6281+
}
6282+
61696283
CHAKRA_API JsTraceExternalReference(_In_ JsRuntimeHandle runtimeHandle, _In_ JsValueRef value)
61706284
{
61716285
return GlobalAPIWrapper_NoRecord([&]() -> JsErrorCode {

lib/Jsrt/JsrtCommonExports.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
JsCreateCustomExternalObjectWithPrototype
3636
JsCreateTracedCustomExternalObjectWithPrototype
3737
JsCreateTracedCustomExternalObjectWithPrototypeAndSlots
38+
JsGetArrayForEachFunction
39+
JsGetArrayKeysFunction
40+
JsGetArrayValuesFunction
41+
JsGetArrayEntriesFunction
42+
JsGetPropertyIdSymbolIterator
43+
JsGetErrorPrototype
44+
JsGetIteratorPrototype
3845
JsConvertValueToObject
3946
JsGetPrototype
4047
JsSetPrototype

lib/Runtime/Library/JavascriptExceptionMetadata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ namespace Js {
105105

106106
if (nextLine >= cache->GetLineCount())
107107
{
108-
endByteOffset = functionBody->LengthInBytes();
109-
endCharOffset = functionBody->LengthInChars();
108+
endByteOffset = startByteOffset + functionBody->LengthInBytes();
109+
endCharOffset = startCharOffset + functionBody->LengthInChars();
110110
}
111111
else
112112
{

0 commit comments

Comments
 (0)