Skip to content

Commit

Permalink
Exposing APIs to store/get host data
Browse files Browse the repository at this point in the history
This is implemented by using internal property (EmbedderData). Currently
it is used for TypedArray/ArrayBuffer. It may expand in future.
  • Loading branch information
akroshg committed Mar 12, 2019
1 parent 1736e5b commit 62d3d11
Show file tree
Hide file tree
Showing 13 changed files with 4,874 additions and 4,775 deletions.
24 changes: 24 additions & 0 deletions lib/Jsrt/ChakraCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,30 @@ CHAKRA_API
JsExternalizeArrayBuffer(
_In_ JsValueRef arrayBuffer);

/// <summary>
/// Get host embedded data from the current object
/// </summary>
/// <param name="instance">Js object from which an embedder data to be fetched</param>
/// <param name="embedderData">An embedder data to be returned, it will be nullptr if not found</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code
/// otherwise.
/// </returns>
CHAKRA_API
JsGetEmbedderData(_In_ JsValueRef instance, _Out_ JsValueRef* embedderData);

/// <summary>
/// Set host embedded data on the current object
/// </summary>
/// <param name="instance">Js object from which an embedder data to be fetched</param>
/// <param name="embedderData">An embedder data to be set on the passed object</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code
/// otherwise.
/// </returns>
CHAKRA_API
JsSetEmbedderData(_In_ JsValueRef instance, _In_ JsValueRef embedderData);

#ifdef _WIN32
#include "ChakraCoreWindows.h"
#endif // _WIN32
Expand Down
57 changes: 57 additions & 0 deletions lib/Jsrt/Core/JsrtCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,63 @@ JsSetArrayBufferExtraInfo(
END_JSRT_NO_EXCEPTION
}


CHAKRA_API
JsGetEmbedderData(
_In_ JsValueRef instance,
_Out_ JsValueRef* embedderData)
{
VALIDATE_JSREF(instance);
PARAM_NOT_NULL(embedderData);
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext* scriptContext) -> JsErrorCode {
Js::RecyclableObject* object = Js::JavascriptOperators::TryFromVar<Js::RecyclableObject>(instance);
if (!object)
{
return JsErrorInvalidArgument;
}

// Right now we know that we support these many. Lets find out if
// there are more.
Assert(Js::TypedArrayBase::Is(object->GetTypeId()) ||
object->GetTypeId() == Js::TypeIds_ArrayBuffer ||
object->GetTypeId() == Js::TypeIds_DataView ||
object->GetTypeId() == Js::TypeIds_SharedArrayBuffer);

if (!object->GetInternalProperty(object, Js::InternalPropertyIds::EmbedderData, embedderData, nullptr, scriptContext))
{
*embedderData = nullptr;
}
return JsNoError;
});
}

CHAKRA_API
JsSetEmbedderData(_In_ JsValueRef instance, _In_ JsValueRef embedderData)
{
VALIDATE_JSREF(instance);
return ContextAPINoScriptWrapper_NoRecord([&](Js::ScriptContext* scriptContext) -> JsErrorCode {
Js::RecyclableObject* object = Js::JavascriptOperators::TryFromVar<Js::RecyclableObject>(instance);
if (!object)
{
return JsErrorInvalidArgument;
}

// Right now we know that we support these many. Lets find out if
// there are more.
Assert(Js::TypedArrayBase::Is(object->GetTypeId()) ||
object->GetTypeId() == Js::TypeIds_ArrayBuffer ||
object->GetTypeId() == Js::TypeIds_DataView ||
object->GetTypeId() == Js::TypeIds_SharedArrayBuffer);

if (!object->SetInternalProperty(Js::InternalPropertyIds::EmbedderData, embedderData, Js::PropertyOperationFlags::PropertyOperation_None, nullptr))
{
return JsErrorInvalidArgument;
}

return JsNoError;
});
}

CHAKRA_API
JsExternalizeArrayBuffer(
_In_ JsValueRef arrayBufferVar)
Expand Down
2 changes: 2 additions & 0 deletions lib/Jsrt/JsrtCommonExports.inc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
JsGetIteratorPrototype
JsGetPropertyIdSymbolIterator
JsGetWeakReferenceValue
JsGetEmbedderData
JsSetEmbedderData
JsHasOwnProperty
JsHasOwnItem
JsIsCallable
Expand Down
2 changes: 2 additions & 0 deletions lib/Runtime/InternalPropertyList.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ INTERNALPROPERTY(CachedUDateFormat) // Used to store cached UDateF
INTERNALPROPERTY(CachedUPluralRules) // Used to store cached UPluralRules objects for Intl.PluralRules
INTERNALPROPERTY(RevocableProxy) // Internal slot for [[RevokableProxy]] for revocable proxy in ES6
INTERNALPROPERTY(MutationBp) // Used to store strong reference to the mutation breakpoint object
INTERNALPROPERTY(EmbedderData) // Holds embedder data here.

#undef INTERNALPROPERTY
2,142 changes: 1,071 additions & 1,071 deletions lib/Runtime/Library/InJavascript/Intl.js.bc.32b.h

Large diffs are not rendered by default.

2,138 changes: 1,069 additions & 1,069 deletions lib/Runtime/Library/InJavascript/Intl.js.bc.64b.h

Large diffs are not rendered by default.

2,166 changes: 1,083 additions & 1,083 deletions lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.32b.h

Large diffs are not rendered by default.

2,124 changes: 1,062 additions & 1,062 deletions lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.64b.h

Large diffs are not rendered by default.

244 changes: 122 additions & 122 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.bc.32b.h

Large diffs are not rendered by default.

244 changes: 122 additions & 122 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.bc.64b.h

Large diffs are not rendered by default.

246 changes: 123 additions & 123 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.nojit.bc.32b.h

Large diffs are not rendered by default.

246 changes: 123 additions & 123 deletions lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js.nojit.bc.64b.h

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions lib/Runtime/Types/DynamicObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,16 @@ namespace Js
mutationBpValue = nullptr;
}

Var embedderData = nullptr;
if (this->GetInternalProperty(this, InternalPropertyIds::EmbedderData, &embedderData, nullptr, this->GetScriptContext()))
{
this->SetInternalProperty(InternalPropertyIds::EmbedderData, nullptr, PropertyOperation_Force, nullptr);
}
else
{
embedderData = nullptr;
}

// If value of TypeOfPrototypeObjectDictionary was set undefined above, reset it to nullptr so we don't type cast it wrongly to TypeTransitionMap* or we don't marshal the non-Var dictionary below
Var typeTransitionMap = nullptr;
if (this->GetInternalProperty(this, InternalPropertyIds::TypeOfPrototypeObjectDictionary, &typeTransitionMap, nullptr, this->GetScriptContext()))
Expand All @@ -832,6 +842,10 @@ namespace Js
{
this->SetInternalProperty(InternalPropertyIds::MutationBp, mutationBpValue, PropertyOperation_Force, nullptr);
}
if (embedderData)
{
this->SetInternalProperty(InternalPropertyIds::EmbedderData, embedderData, PropertyOperation_Force, nullptr);
}
}
}

Expand Down

0 comments on commit 62d3d11

Please sign in to comment.