Skip to content

Jsrt: new string API#1830

Merged
chakrabot merged 1 commit into
chakra-core:masterfrom
obastemur:string_api
Nov 4, 2016
Merged

Jsrt: new string API#1830
chakrabot merged 1 commit into
chakra-core:masterfrom
obastemur:string_api

Conversation

@obastemur

@obastemur obastemur commented Oct 26, 2016

Copy link
Copy Markdown
Collaborator
  • 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 ]

@obastemur obastemur force-pushed the string_api branch 22 times, most recently from e7a9e71 to 45bfcd6 Compare October 27, 2016 15:51
Comment thread bin/ch/ChakraRtInterface.h Outdated
@@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit pick: please place CreateStringUtf8/WriteStringUtf8 next to each other.

Comment thread bin/ch/WScriptJsrt.cpp Outdated

errorCode = ChakraRTInterface::JsRunScriptUtf8(fileContent, GetNextSourceContext(), fullPathNarrow, &returnValue);
JsValueRef scriptSource;
ChakraRTInterface::JsCreateExternalArrayBuffer((void*)fileContent, (unsigned int)strlen(fileContent),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChakraRTInterface::JsCreateExternalArrayBuffer [](start = 8, length = 46)

Some new code in this file like this may use handle error / IfJsrtErrorSetGo.

Comment thread bin/ch/WScriptJsrt.cpp Outdated
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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have "char*" version as well, should we use that or stay away from it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Char version for ASCII input

Comment thread bin/ch/stdafx.h Outdated
{
return;
size_t len = 0;
errorCode = ChakraRTInterface::JsWriteStringUtf8(strValue, nullptr, 0, &len);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have test/native-test for this purpose. Prefer a sample there

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see new native-test test-char16.

/cc @liminzhu . We need to update Chakra-Samples repo parallel to this PR.

Comment thread lib/Jsrt/ChakraCommon.h Outdated
/// ChakraCore assumes ExternalArrayBuffer is Utf8 by default.
/// This one needs to be set for Utf16
/// </summary>
JsParseScriptAttributeUtf16Encoding = 0x2,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being explicit is better, though that will make this a long name but we should make it JsParseScriptAttributeArrayBufferIsUtf16Encoded


In reply to: 85450903 [](ancestors = 85450903)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay two vs one. Updating to JsParseScriptAttributeArrayBufferIsUtf16Encoded

Comment thread lib/Jsrt/ChakraCore.h Outdated
/// </returns>
typedef bool (CHAKRA_CALLBACK * JsSerializedLoadScriptCallback)
(JsSourceContext sourceContext, JsValueRef *value,
JsParseScriptAttributes *parseAttributes);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing SAL annotations

@obastemur obastemur force-pushed the string_api branch 2 times, most recently from d947b7d to d79da15 Compare November 2, 2016 23:12
Comment thread lib/Jsrt/ChakraCore.h Outdated
_In_ JsValueRef value,
_Out_opt_ uint8_t* buffer,
_In_ size_t bufferSize,
_Out_opt_ size_t* length);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

length [](start = 26, length = 6)

Change to written, to be consistent with other APIs

@obastemur obastemur force-pushed the string_api branch 7 times, most recently from fd8e305 to 6c90306 Compare November 3, 2016 23:48
Comment thread lib/Jsrt/ChakraCommon.h
JsParseScriptAttributeLibraryCode = 0x1,
/// <summary>
/// ChakraCore assumes ExternalArrayBuffer is Utf8 by default.
/// This one needs to be set for Utf16

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: description may need to mention parse script context, otherwise reading by itself is confusing (why ExternalArrayBuffer has anything to do with utf8).

Comment thread lib/Jsrt/ChakraCore.h Outdated
/// <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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment outdated (copied from old?)

Comment thread lib/Jsrt/ChakraCore.h Outdated
/// 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>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment outdated (passed to "API names" changed)

Comment thread lib/Jsrt/ChakraCore.h Outdated
/// </para>
/// </remarks>
/// <param name="content">Pointer to string memory.</param>
/// <param name="length">Number of bytes within the string</param>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume "length" is number of characters, not number of bytes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is number of bytes not chars.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it is number of characters.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it is number of characters.

Appreciate if you can point me to source code showing that.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate if you can point me to source code showing that.

Track down the implementation. It calls JsPointerToString. And that length is "stringLength", cchLength, ...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..

Comment thread lib/Jsrt/ChakraCore.h
/// </para>
/// </remarks>
/// <param name="content">Pointer to string memory.</param>
/// <param name="length">Number of bytes within the string</param>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider "number of characters"?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also number of bytes and actually same thing for this one

Comment thread lib/Jsrt/ChakraCore.h Outdated
_Out_ JsValueRef *result);

/// <summary>
/// Gets the property ID associated with the name.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2 PropertyID APIs may have a different description than before, now that the API names don't contain "Get".

@jianchun

jianchun commented Nov 4, 2016

Copy link
Copy Markdown

LGTM. Left some comments on API descriptions. Maybe @liminzhu could go through "ChakraCore.h" and help with the API docs?

@obastemur

Copy link
Copy Markdown
Collaborator Author

Did some extra cleanup to old comments.

Maybe @liminzhu could go through "ChakraCore.h" and help with the API docs?

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.
@obastemur

Copy link
Copy Markdown
Collaborator Author

@jianchun @agarwal-sandeep @liminzhu Thanks for the review and discussion.

@obastemur

Copy link
Copy Markdown
Collaborator Author

@dotnet-bot test Windows x86_debug please

@chakrabot chakrabot merged commit f7f7132 into chakra-core:master Nov 4, 2016
chakrabot pushed a commit that referenced this pull request Nov 4, 2016
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 ]
@kphillisjr

Copy link
Copy Markdown
Contributor

This pull request is incomplete. The ChakraCore Samples needs updated. See this Issue: microsoft/Chakra-Samples#47

@obastemur

Copy link
Copy Markdown
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants