Skip to content

Commit 62d3d11

Browse files
committed
Exposing APIs to store/get host data
This is implemented by using internal property (EmbedderData). Currently it is used for TypedArray/ArrayBuffer. It may expand in future.
1 parent 1736e5b commit 62d3d11

13 files changed

+4874
-4775
lines changed

lib/Jsrt/ChakraCore.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,30 @@ CHAKRA_API
19491949
JsExternalizeArrayBuffer(
19501950
_In_ JsValueRef arrayBuffer);
19511951

1952+
/// <summary>
1953+
/// Get host embedded data from the current object
1954+
/// </summary>
1955+
/// <param name="instance">Js object from which an embedder data to be fetched</param>
1956+
/// <param name="embedderData">An embedder data to be returned, it will be nullptr if not found</param>
1957+
/// <returns>
1958+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code
1959+
/// otherwise.
1960+
/// </returns>
1961+
CHAKRA_API
1962+
JsGetEmbedderData(_In_ JsValueRef instance, _Out_ JsValueRef* embedderData);
1963+
1964+
/// <summary>
1965+
/// Set host embedded data on the current object
1966+
/// </summary>
1967+
/// <param name="instance">Js object from which an embedder data to be fetched</param>
1968+
/// <param name="embedderData">An embedder data to be set on the passed object</param>
1969+
/// <returns>
1970+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code
1971+
/// otherwise.
1972+
/// </returns>
1973+
CHAKRA_API
1974+
JsSetEmbedderData(_In_ JsValueRef instance, _In_ JsValueRef embedderData);
1975+
19521976
#ifdef _WIN32
19531977
#include "ChakraCoreWindows.h"
19541978
#endif // _WIN32

lib/Jsrt/Core/JsrtCore.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,63 @@ JsSetArrayBufferExtraInfo(
497497
END_JSRT_NO_EXCEPTION
498498
}
499499

500+
501+
CHAKRA_API
502+
JsGetEmbedderData(
503+
_In_ JsValueRef instance,
504+
_Out_ JsValueRef* embedderData)
505+
{
506+
VALIDATE_JSREF(instance);
507+
PARAM_NOT_NULL(embedderData);
508+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext* scriptContext) -> JsErrorCode {
509+
Js::RecyclableObject* object = Js::JavascriptOperators::TryFromVar<Js::RecyclableObject>(instance);
510+
if (!object)
511+
{
512+
return JsErrorInvalidArgument;
513+
}
514+
515+
// Right now we know that we support these many. Lets find out if
516+
// there are more.
517+
Assert(Js::TypedArrayBase::Is(object->GetTypeId()) ||
518+
object->GetTypeId() == Js::TypeIds_ArrayBuffer ||
519+
object->GetTypeId() == Js::TypeIds_DataView ||
520+
object->GetTypeId() == Js::TypeIds_SharedArrayBuffer);
521+
522+
if (!object->GetInternalProperty(object, Js::InternalPropertyIds::EmbedderData, embedderData, nullptr, scriptContext))
523+
{
524+
*embedderData = nullptr;
525+
}
526+
return JsNoError;
527+
});
528+
}
529+
530+
CHAKRA_API
531+
JsSetEmbedderData(_In_ JsValueRef instance, _In_ JsValueRef embedderData)
532+
{
533+
VALIDATE_JSREF(instance);
534+
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext* scriptContext) -> JsErrorCode {
535+
Js::RecyclableObject* object = Js::JavascriptOperators::TryFromVar<Js::RecyclableObject>(instance);
536+
if (!object)
537+
{
538+
return JsErrorInvalidArgument;
539+
}
540+
541+
// Right now we know that we support these many. Lets find out if
542+
// there are more.
543+
Assert(Js::TypedArrayBase::Is(object->GetTypeId()) ||
544+
object->GetTypeId() == Js::TypeIds_ArrayBuffer ||
545+
object->GetTypeId() == Js::TypeIds_DataView ||
546+
object->GetTypeId() == Js::TypeIds_SharedArrayBuffer);
547+
548+
if (!object->SetInternalProperty(Js::InternalPropertyIds::EmbedderData, embedderData, Js::PropertyOperationFlags::PropertyOperation_None, nullptr))
549+
{
550+
return JsErrorInvalidArgument;
551+
}
552+
553+
return JsNoError;
554+
});
555+
}
556+
500557
CHAKRA_API
501558
JsExternalizeArrayBuffer(
502559
_In_ JsValueRef arrayBufferVar)

lib/Jsrt/JsrtCommonExports.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
JsGetIteratorPrototype
135135
JsGetPropertyIdSymbolIterator
136136
JsGetWeakReferenceValue
137+
JsGetEmbedderData
138+
JsSetEmbedderData
137139
JsHasOwnProperty
138140
JsHasOwnItem
139141
JsIsCallable

lib/Runtime/InternalPropertyList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ INTERNALPROPERTY(CachedUDateFormat) // Used to store cached UDateF
2626
INTERNALPROPERTY(CachedUPluralRules) // Used to store cached UPluralRules objects for Intl.PluralRules
2727
INTERNALPROPERTY(RevocableProxy) // Internal slot for [[RevokableProxy]] for revocable proxy in ES6
2828
INTERNALPROPERTY(MutationBp) // Used to store strong reference to the mutation breakpoint object
29+
INTERNALPROPERTY(EmbedderData) // Holds embedder data here.
30+
2931
#undef INTERNALPROPERTY

lib/Runtime/Library/InJavascript/Intl.js.bc.32b.h

Lines changed: 1071 additions & 1071 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/InJavascript/Intl.js.bc.64b.h

Lines changed: 1069 additions & 1069 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.32b.h

Lines changed: 1083 additions & 1083 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.64b.h

Lines changed: 1062 additions & 1062 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.bc.32b.h

Lines changed: 122 additions & 122 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.bc.64b.h

Lines changed: 122 additions & 122 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)