Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsrt: new string API #1830

Merged
merged 1 commit into from
Nov 4, 2016
Merged

Jsrt: new string API #1830

merged 1 commit into from
Nov 4, 2016

Conversation

obastemur
Copy link
Collaborator

@obastemur obastemur commented Oct 26, 2016

  • 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 ]

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

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

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.

@@ -414,7 +409,13 @@ JsValueRef WScriptJsrt::LoadScript(JsValueRef callee, LPCSTR fileName, LPCSTR fi

IfJsrtErrorSetGo(ChakraRTInterface::JsSetCurrentContext(calleeContext));

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

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.

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,

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

{
return;
size_t len = 0;
errorCode = ChakraRTInterface::JsWriteStringUtf8(strValue, nullptr, 0, &len);

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

/// ChakraCore assumes ExternalArrayBuffer is Utf8 by default.
/// This one needs to be set for Utf16
/// </summary>
JsParseScriptAttributeUtf16Encoding = 0x2,

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

/// </returns>
typedef bool (CHAKRA_CALLBACK * JsSerializedLoadScriptCallback)
(JsSourceContext sourceContext, JsValueRef *value,
JsParseScriptAttributes *parseAttributes);

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
_In_ JsValueRef value,
_Out_opt_ uint8_t* buffer,
_In_ size_t bufferSize,
_Out_opt_ size_t* length);
Copy link
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
JsParseScriptAttributeLibraryCode = 0x1,
/// <summary>
/// ChakraCore assumes ExternalArrayBuffer is Utf8 by default.
/// This one needs to be set for Utf16
Copy link

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

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

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

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

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)

/// </para>
/// </remarks>
/// <param name="content">Pointer to string memory.</param>
/// <param name="length">Number of bytes within the string</param>
Copy link

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

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

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

/// </para>
/// </remarks>
/// <param name="content">Pointer to string memory.</param>
/// <param name="length">Number of bytes within the string</param>
Copy link

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

_Out_ JsValueRef *result);

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

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
Copy link

jianchun commented Nov 4, 2016

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

@obastemur
Copy link
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
Collaborator Author

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

@obastemur
Copy link
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
Contributor

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

@obastemur
Copy link
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