Add JSRT basic tasks tutorials#44
Conversation
|
@dilijev fyi |
|
|
||
| ## Modules | ||
| ES6 Modules are supported in ChakraCore. However, the host needs to specify how to resolve a module specifier since this part is host-specific not runtime-related. JSRT provides experimental APIs to set up ES6 modules. This [PR](https://github.com/Microsoft/ChakraCore/commit/50767f9f33812b4a036794a6a98404145563409f) gives you an overview of the related APIs and an example implementation (see WScriptJsrt.cpp) in the ch test host. Better documentation will be provided in the future. | ||
|
|
There was a problem hiding this comment.
For the PR you might want to link to the actual PR instead of the commit
| } | ||
| wprintf(L"\n"); | ||
| return JS_INVALID_REFERENCE; | ||
| } |
There was a problem hiding this comment.
The code above uses tabs while the rest uses 4 spaces. We use 4 spaces in ChakraCore so we should probably unify to that.
| // load script when necessary | ||
| *scriptBuffer = LoadScript(((SerializedSourceContext*)sourceContext)->scriptPath); | ||
| return true; | ||
| }, |
There was a problem hiding this comment.
Should this whole block (starting from open brace line 186) be indented one level deeper (to line up with [] starting the lambda)?
| [](JsSourceContext sourceContext) | ||
| { | ||
| // free resources | ||
| }, |
There was a problem hiding this comment.
Same comment re indentation.
| JsSetProperty(global, consolePropId, console, true); | ||
| ``` | ||
|
|
||
| Now in JavaScript, you can do, |
There was a problem hiding this comment.
nit: change the trailing , to :
There was a problem hiding this comment.
nit: also add a newline before the code block
| console.log('Hello world'); | ||
| ``` | ||
|
|
||
| If you need to expose an object that hosts data not directly exposed to JS, you can create an external object via `JsCreateExternalObject`. |
There was a problem hiding this comment.
Should we show an example of this?
There was a problem hiding this comment.
I feel like the API shape is pretty self-explanatory so I left that out.
There was a problem hiding this comment.
I'm not sure that this line is helpful without an example at least illustrating what type of data you might expose and why you might expose it.
There was a problem hiding this comment.
Please also consider that we have JsCreateExternalObjectWithPrototype . IMHO we may show an example of that one instead (usage is available under test/native-tests)
There was a problem hiding this comment.
@obastemur I didn't know that's a thing! Will look into it.
|
|
||
| ### Script serialization with lazy source loading | ||
|
|
||
| To support startup and execution efficiency, JSRT APIs provide the capability of pre-parsing, generating syntax trees and caching the bytecode for scripts. It is especially useful for server scenarios where the same script could get executed for thousands of times. |
There was a problem hiding this comment.
nit: executed for thousands of times -> executed thousands of times
|
|
||
| The basic idea is to use variants of `JsSerializeScript` to parse and store the bytecode and then use `JsParse/RunSerializedScript` to parse or run the bytecode. Source is typically not needed for running serialized code, therefore you can also lazy-load the source script using `JsRunSerialized/JsRunSerializedScriptWithCallback`. | ||
|
|
||
|
|
| JsValueRef result; | ||
| unsigned long bufferSize = 0; | ||
| BYTE *buffer = nullptr; | ||
| JsSerializeScript(script, buffer, &bufferSize); // get correct buffer size |
There was a problem hiding this comment.
nit: to make this clearer (passing nullptr for buffer argument to get size without filling a buffer or getting an error about buffer too small), replace buffer with nullptr in this line.
| [](JsSourceContext sourceContext, const WCHAR** scriptBuffer) | ||
| { | ||
| // load script when necessary | ||
| *scriptBuffer = LoadScript(((SerializedSourceContext*)sourceContext)->scriptPath); |
There was a problem hiding this comment.
Are LoadScript and LoadByteCode visible to user code through JSRT or are these stubs not defined in this example?
There was a problem hiding this comment.
They are pseudo-code methods user need to define
|
Thanks for the comments @dilijev ! |
| JsConvertValueToString(arguments[index], &stringValue); | ||
| const wchar_t *string; | ||
| size_t length; | ||
| JsStringToPointer(stringValue, &string, &length); |
There was a problem hiding this comment.
Do we want these examples to also work cross-plat? For instance JsStringToPointer isn't exposed on Linux/Mac, instead JsCopyString needs to be used.
| // create console object, log function, and set log function as property of console | ||
| JsCreateObject(&console); | ||
| JsCreateFunction(LogCB, nullptr, &logFunc); | ||
| JsGetPropertyIdFromName(L"log", &logPropId); |
There was a problem hiding this comment.
JsGetPropertyIdFromName is also only available on Windows, JsCreatePropertyId should be used instead.
kfarnung
left a comment
There was a problem hiding this comment.
LGTM keeping in mind that the examples will only run on Windows. I'd recommend that another pass it made to ensure that all examples work cross-platform.
| JsValueRef result; | ||
| unsigned long bufferSize = 0; | ||
| BYTE *buffer = nullptr; | ||
| JsSerializeScript(script, nullptr, &bufferSize); // get correct buffer size |
There was a problem hiding this comment.
This is also not ChakraCore. Use JsSerialize please.
|
I will crossplat-fy all the samples. Thanks for the suggestion @kfarnung @obastemur . |
|
|
||
| // load and run bytecode | ||
| void runSerializeScript(const wchar_t *bytecodeBufferPath, const wchar_t *scriptBufferPath) { | ||
| // load and run bytecode |
There was a problem hiding this comment.
nit: looks like accidental space
|
All feedback addressed and ready to merge if yall have no additional feedback. Thanks guys! |
Per #5, I'm adding some tutorials for JSRT. I'm putting the content in JSRT Overview before I find a better spot (feel free to make suggestions).
This PR adds illustration for
And another temporary section for