Jsrt: new string API#1830
Conversation
e7a9e71 to
45bfcd6
Compare
| @@ -25,7 +25,6 @@ struct JsAPIHooks | |||
| typedef JsErrorCode (WINAPI *JsrtConvertValueToBooleanPtr)(JsValueRef value, JsValueRef *booleanValue); | |||
| typedef JsErrorCode (WINAPI *JsrtStringToPointerUtf8CopyPtr)(JsValueRef value, char **stringValue, size_t *length); | |||
There was a problem hiding this comment.
typedef JsErrorCode (WINAPI _JsrtStringToPointerUtf8CopyPtr)(JsValueRef value, char *_stringValue, size_t *length); [](start = 3, length = 116)
JsrtStringToPointerUtf8CopyPtr not needed?
| typedef JsErrorCode(WINAPI *JsrtWriteStringUtf8)(JsValueRef value, uint8_t* buffer, size_t bufferSize, size_t* written); | ||
| typedef JsErrorCode(WINAPI *JsrtCreatePropertyIdUtf8)(const char *name, size_t length, JsPropertyIdRef *propertyId); | ||
| typedef JsErrorCode(WINAPI *JsrtCreateStringUtf8)(const uint8_t *content, size_t length, JsValueRef *value); | ||
| typedef JsErrorCode(WINAPI *JsrtCreateExternalArrayBuffer)(void *data, unsigned int byteLength, JsFinalizeCallback finalizeCallback, void *callbackState, JsValueRef *result); |
There was a problem hiding this comment.
nit pick: please place CreateStringUtf8/WriteStringUtf8 next to each other.
|
|
||
| errorCode = ChakraRTInterface::JsRunScriptUtf8(fileContent, GetNextSourceContext(), fullPathNarrow, &returnValue); | ||
| JsValueRef scriptSource; | ||
| ChakraRTInterface::JsCreateExternalArrayBuffer((void*)fileContent, (unsigned int)strlen(fileContent), |
There was a problem hiding this comment.
ChakraRTInterface::JsCreateExternalArrayBuffer [](start = 8, length = 46)
Some new code in this file like this may use handle error / IfJsrtErrorSetGo.
| IfJsrtErrorFail(ChakraRTInterface::JsPointerToStringUtf8(CPU_ARCH_TEXT,strlen(CPU_ARCH_TEXT), &archValue), false); | ||
| IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(platformObject, archProperty, archValue, true), false); | ||
| IfJsrtErrorFail(ChakraRTInterface::JsCreateStringUtf8( | ||
| (const uint8_t*)CPU_ARCH_TEXT, |
There was a problem hiding this comment.
We have "char*" version as well, should we use that or stay away from it?
There was a problem hiding this comment.
Char version for ASCII input
| { | ||
| return; | ||
| size_t len = 0; | ||
| errorCode = ChakraRTInterface::JsWriteStringUtf8(strValue, nullptr, 0, &len); |
There was a problem hiding this comment.
This is fine, though we have JsWriteStringUtf16. In case the user instantiate this class only to GetWideString(), should we consider delay reading string content and also exercise JsWriteStringUtf16? May be a good chance to test out that API.
There was a problem hiding this comment.
We have test/native-test for this purpose. Prefer a sample there
There was a problem hiding this comment.
see new native-test test-char16.
/cc @liminzhu . We need to update Chakra-Samples repo parallel to this PR.
| /// ChakraCore assumes ExternalArrayBuffer is Utf8 by default. | ||
| /// This one needs to be set for Utf16 | ||
| /// </summary> | ||
| JsParseScriptAttributeUtf16Encoding = 0x2, |
There was a problem hiding this comment.
JsParseScriptAttributeUtf16Encoding [](start = 8, length = 35)
The comment is clear, but users may possibly be confused by the name and think this flag always apply? Should we explicitly have "ArrayBuffer" in the flag name? Or not?
There was a problem hiding this comment.
Being explicit is better, though that will make this a long name but we should make it JsParseScriptAttributeArrayBufferIsUtf16Encoded
In reply to: 85450903 [](ancestors = 85450903)
There was a problem hiding this comment.
Okay two vs one. Updating to JsParseScriptAttributeArrayBufferIsUtf16Encoded
| /// </returns> | ||
| typedef bool (CHAKRA_CALLBACK * JsSerializedLoadScriptCallback) | ||
| (JsSourceContext sourceContext, JsValueRef *value, | ||
| JsParseScriptAttributes *parseAttributes); |
d947b7d to
d79da15
Compare
| _In_ JsValueRef value, | ||
| _Out_opt_ uint8_t* buffer, | ||
| _In_ size_t bufferSize, | ||
| _Out_opt_ size_t* length); |
There was a problem hiding this comment.
length [](start = 26, length = 6)
Change to written, to be consistent with other APIs
fd8e305 to
6c90306
Compare
| JsParseScriptAttributeLibraryCode = 0x1, | ||
| /// <summary> | ||
| /// ChakraCore assumes ExternalArrayBuffer is Utf8 by default. | ||
| /// This one needs to be set for Utf16 |
There was a problem hiding this comment.
nit: description may need to mention parse script context, otherwise reading by itself is confusing (why ExternalArrayBuffer has anything to do with utf8).
| /// <summary> | ||
| /// Called by the runtime to load the source code of the serialized script. | ||
| /// The caller must keep the script buffer valid until the JsSerializedScriptUnloadCallback. | ||
| /// This callback is only supported by the Win32 version of the API |
| /// The caller must keep the script buffer valid until the JsSerializedScriptUnloadCallback. | ||
| /// This callback is only supported by the Win32 version of the API | ||
| /// </summary> | ||
| /// <param name="sourceContext">The context passed to Js[Parse|Run]SerializedScriptWithCallback</param> |
There was a problem hiding this comment.
Comment outdated (passed to "API names" changed)
| /// </para> | ||
| /// </remarks> | ||
| /// <param name="content">Pointer to string memory.</param> | ||
| /// <param name="length">Number of bytes within the string</param> |
There was a problem hiding this comment.
I assume "length" is number of characters, not number of bytes.
There was a problem hiding this comment.
It is number of bytes not chars.
There was a problem hiding this comment.
No, I think it is number of characters.
There was a problem hiding this comment.
No, I think it is number of characters.
Appreciate if you can point me to source code showing that.
There was a problem hiding this comment.
Appreciate if you can point me to source code showing that.
Track down the implementation. It calls JsPointerToString. And that length is "stringLength", cchLength, ...
There was a problem hiding this comment.
On the email it shows JsCreateStringUtf8 on top... followed the line from here and what you mean here is Utf16... Yes for Utf16, it is the length of string..
| /// </para> | ||
| /// </remarks> | ||
| /// <param name="content">Pointer to string memory.</param> | ||
| /// <param name="length">Number of bytes within the string</param> |
There was a problem hiding this comment.
This is also number of bytes and actually same thing for this one
| _Out_ JsValueRef *result); | ||
|
|
||
| /// <summary> | ||
| /// Gets the property ID associated with the name. |
There was a problem hiding this comment.
The 2 PropertyID APIs may have a different description than before, now that the API names don't contain "Get".
|
LGTM. Left some comments on API descriptions. Maybe @liminzhu could go through "ChakraCore.h" and help with the API docs? |
|
Did some extra cleanup to old comments.
We may better decide on remaining types etc. part first (before updating anything again). Changing design again and again on the PR is a huge pain. |
- JsCreateString - JsCreateStringUtf8 - JsCreateStringUtf16 - JsCopyString - JsCopyStringUtf8 - JsCopyStringUtf16 - JsParse - JsRun - JsSerialize - JsParseSerialized - JsRunSerialized - JsCreatePropertyIdUtf8 - JsCopyPropertyIdUtf8 With this update, `ExternalArrayBuffer` can be used as script source. When ExternalArrayBuffer source is Utf8, it is used as is and keeps script source from being copied.
|
@jianchun @agarwal-sandeep @liminzhu Thanks for the review and discussion. |
|
@dotnet-bot test Windows x86_debug please |
Merge pull request #1830 from obastemur:string_api - JsCreateString - JsCreateStringUtf8 - JsCreateStringUtf16 - JsCopyString - JsCopyStringUtf8 - JsCopyStringUtf16 - JsParse - JsRun - JsSerialize - JsParseSerialized - JsRunSerialized - JsCreatePropertyIdUtf8 - JsCopyPropertyIdUtf8 New: `ExternalArrayBuffer` can be used as script source. In case the ExternalArrayBuffer source is Utf8, CC uses it as is. no additional copy/encode/decode.. p.s. [ Create/Copy String credits goes to @jianchun ]
|
This pull request is incomplete. The ChakraCore Samples needs updated. See this Issue: microsoft/Chakra-Samples#47 |
New:
ExternalArrayBuffercan be used as script source.In case the ExternalArrayBuffer source is Utf8, CC uses it as is.
no additional copy/encode/decode..
p.s. [ Create/Copy String credits goes to @jianchun ]