This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chakrashim: correct Template properties support
Template (ObjectTemplate, FunctionTemplate) properties can be Templates. When creating Template instances, Template properties need to be replaced with corresponding Template instantiations. `nodejs/master` starts to depend on this feature.
- Loading branch information
Jianchun Xu
committed
Apr 26, 2016
1 parent
acca384
commit 23e9838
Showing
9 changed files
with
480 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright Microsoft. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files(the "Software"), to | ||
// deal in the Software without restriction, including without limitation the | ||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and / or | ||
// sell copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions : | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
// IN THE SOFTWARE. | ||
|
||
#include "v8chakra.h" | ||
|
||
namespace v8 { | ||
|
||
using jsrt::IsolateShim; | ||
|
||
Object* TemplateData::EnsureProperties() { | ||
if (properties.IsEmpty()) { | ||
properties = Object::New(); | ||
} | ||
|
||
return *properties; | ||
} | ||
|
||
bool TemplateData::Is(ExternalData* data) { | ||
return data->GetType() == ExternalDataTypes::ObjectTemplateData | ||
|| data->GetType() == ExternalDataTypes::FunctionTemplateData; | ||
} | ||
|
||
// Clone the template properties into the new instance | ||
JsErrorCode TemplateData::CopyPropertiesTo(JsValueRef newInstance) { | ||
if (properties.IsEmpty()) { | ||
return JsNoError; | ||
} | ||
|
||
JsValueRef propertiesRef = *properties; | ||
JsValueRef propertyNames; | ||
IfJsErrorRet(JsGetOwnPropertyNames(propertiesRef, &propertyNames)); | ||
|
||
unsigned int length; | ||
IfJsErrorRet(jsrt::GetArrayLength(propertyNames, &length)); | ||
|
||
JsPropertyIdRef valueIdRef; | ||
if (length > 0) { | ||
valueIdRef = IsolateShim::GetCurrent()->GetCachedPropertyIdRef( | ||
jsrt::CachedPropertyIdRef::value); | ||
} | ||
|
||
for (unsigned int index = 0; index < length; index++) { | ||
JsValueRef indexValue; | ||
IfJsErrorRet(JsIntToNumber(index, &indexValue)); | ||
|
||
JsValueRef propertyNameValue; | ||
IfJsErrorRet(JsGetIndexedProperty(propertyNames, indexValue, | ||
&propertyNameValue)); | ||
|
||
const wchar_t *propertyName; | ||
size_t propertyNameLength; | ||
IfJsErrorRet(JsStringToPointer(propertyNameValue, | ||
&propertyName, &propertyNameLength)); | ||
|
||
JsPropertyIdRef propertyId; | ||
IfJsErrorRet(JsGetPropertyIdFromName(propertyName, &propertyId)); | ||
|
||
JsValueRef propertyDescriptor; | ||
IfJsErrorRet(JsGetOwnPropertyDescriptor(propertiesRef, propertyId, | ||
&propertyDescriptor)); | ||
|
||
// If it is a Template, try create an instance | ||
JsValueRef value; | ||
IfJsErrorRet(JsGetProperty(propertyDescriptor, valueIdRef, &value)); | ||
ExternalData* data = nullptr; | ||
if (ExternalData::TryGet(value, &data) && TemplateData::Is(data)) { | ||
TemplateData* templateData = static_cast<TemplateData*>(data); | ||
value = templateData->NewInstance(value); | ||
if (value == JS_INVALID_REFERENCE) { | ||
return JsErrorFatal; // Just to indicate failed | ||
} | ||
IfJsErrorRet(JsSetProperty(propertyDescriptor, valueIdRef, value, false)); | ||
} | ||
|
||
bool result; | ||
IfJsErrorRet(JsDefineProperty(newInstance, propertyId, propertyDescriptor, | ||
&result)); | ||
} | ||
|
||
return JsNoError; | ||
} | ||
|
||
} // namespace v8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.