//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
/// \mainpage Chakra Hosting API Reference
///
/// Chakra is Microsoft's JavaScript engine. It is an integral part of Internet Explorer but can
/// also be hosted independently by other applications. This reference describes the APIs available
/// to applications to host Chakra.
///
/// \file
/// \brief The Chakra Core hosting API.
///
/// This file contains a flat C API layer. This is the API exported by ChakraCore.dll.
#ifdef _MSC_VER
#pragma once
#endif // _MSC_VER
#ifndef _CHAKRACORE_H_
#define _CHAKRACORE_H_
#if !defined(NTBUILD) && !defined(_CHAKRACOREBUILD)
#define _CHAKRACOREBUILD
#endif
#include "ChakraCommon.h"
#include "ChakraDebug.h"
// Begin ChakraCore only APIs
#ifdef _CHAKRACOREBUILD
typedef void* JsModuleRecord;
///
/// A reference to an object owned by the SharedArrayBuffer.
///
///
/// This represents SharedContents which is heap allocated object, it can be passed through
/// different runtimes to share the underlying buffer.
///
typedef void *JsSharedArrayBufferContentHandle;
typedef enum JsParseModuleSourceFlags
{
JsParseModuleSourceFlags_DataIsUTF16LE = 0x00000000,
JsParseModuleSourceFlags_DataIsUTF8 = 0x00000001
} JsParseModuleSourceFlags;
typedef enum JsModuleHostInfoKind
{
JsModuleHostInfo_Exception = 0x01,
JsModuleHostInfo_HostDefined = 0x02,
JsModuleHostInfo_NotifyModuleReadyCallback = 0x3,
JsModuleHostInfo_FetchImportedModuleCallback = 0x4,
JsModuleHostInfo_FetchImportedModuleFromScriptCallback = 0x5
} JsModuleHostInfoKind;
///
/// User implemented callback to fetch additional imported modules.
///
///
/// Notify the host to fetch the dependent module. This is the "import" part before HostResolveImportedModule in ES6 spec.
/// This notifies the host that the referencing module has the specified module dependency, and the host need to retrieve the module back.
///
/// The referencing module that is requesting the dependency modules.
/// The specifier coming from the module source code.
/// The ModuleRecord of the dependent module. If the module was requested before from other source, return the
/// existing ModuleRecord, otherwise return a newly created ModuleRecord.
///
/// true if the operation succeeded, false otherwise.
///
typedef JsErrorCode(CHAKRA_CALLBACK * FetchImportedModuleCallBack)(_In_ JsModuleRecord referencingModule, _In_ JsValueRef specifier, _Outptr_result_maybenull_ JsModuleRecord* dependentModuleRecord);
///
/// User implemented callback to get notification when the module is ready.
///
///
/// Notify the host after ModuleDeclarationInstantiation step (15.2.1.1.6.4) is finished. If there was error in the process, exceptionVar
/// holds the exception. Otherwise the referencingModule is ready and the host should schedule execution afterwards.
///
/// The referencing module that have finished running ModuleDeclarationInstantiation step.
/// If nullptr, the module is successfully initialized and host should queue the execution job
/// otherwise it's the exception object.
///
/// true if the operation succeeded, false otherwise.
///
typedef JsErrorCode(CHAKRA_CALLBACK * FetchImportedModuleFromScriptCallBack)(_In_ JsSourceContext dwReferencingSourceContext, _In_ JsValueRef specifier, _Outptr_result_maybenull_ JsModuleRecord* dependentModuleRecord);
///
/// User implemented callback to get notification when the module is ready.
///
///
/// Notify the host after ModuleDeclarationInstantiation step (15.2.1.1.6.4) is finished. If there was error in the process, exceptionVar
/// holds the exception. Otherwise the referencingModule is ready and the host should schedule execution afterwards.
///
/// The referencing script that calls import()
/// If nullptr, the module is successfully initialized and host should queue the execution job
/// otherwise it's the exception object.
///
/// true if the operation succeeded, false otherwise.
///
typedef JsErrorCode(CHAKRA_CALLBACK * NotifyModuleReadyCallback)(_In_opt_ JsModuleRecord referencingModule, _In_opt_ JsValueRef exceptionVar);
///
/// Initialize a ModuleRecord from host
///
///
/// Bootstrap the module loading process by creating a new module record.
///
/// The referencingModule as in HostResolveImportedModule (15.2.1.17). nullptr if this is the top level module.
/// The host normalized specifier. This is the key to a unique ModuleRecord.
/// The new ModuleRecord created. The host should not try to call this API twice with the same normalizedSpecifier.
/// chakra will return an existing ModuleRecord if the specifier was passed in before.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsInitializeModuleRecord(
_In_opt_ JsModuleRecord referencingModule,
_In_ JsValueRef normalizedSpecifier,
_Outptr_result_maybenull_ JsModuleRecord* moduleRecord);
///
/// Parse the module source
///
///
/// This is basically ParseModule operation in ES6 spec. It is slightly different in that the ModuleRecord was initialized earlier, and passed in as an argument.
///
/// The ModuleRecord that holds the parse tree of the source code.
/// A cookie identifying the script that can be used by debuggable script contexts.
/// The source script to be parsed, but not executed in this code.
/// The source length of sourceText. The input might contain embedded null.
/// The type of the source code passed in. It could be UNICODE or utf8 at this time.
/// The error object if there is parse error.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsParseModuleSource(
_In_ JsModuleRecord requestModule,
_In_ JsSourceContext sourceContext,
_In_ BYTE* script,
_In_ unsigned int scriptLength,
_In_ JsParseModuleSourceFlags sourceFlag,
_Outptr_result_maybenull_ JsValueRef* exceptionValueRef);
///
/// Execute module code.
///
///
/// This method implements 15.2.1.1.6.5, "ModuleEvaluation" concrete method.
/// When this methid is called, the chakra engine should have notified the host that the module and all its dependent are ready to be executed.
/// One moduleRecord will be executed only once. Additional execution call on the same moduleRecord will fail.
///
/// The module to be executed.
/// The return value of the module.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsModuleEvaluation(
_In_ JsModuleRecord requestModule,
_Outptr_result_maybenull_ JsValueRef* result);
///
/// Set the host info for the specified module.
///
/// The request module.
/// The type of host info to be set.
/// The host info to be set.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsSetModuleHostInfo(
_In_ JsModuleRecord requestModule,
_In_ JsModuleHostInfoKind moduleHostInfo,
_In_ void* hostInfo);
///
/// Retrieve the host info for the specified module.
///
/// The request module.
/// The type of host info to get.
/// The host info to be retrieved.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsGetModuleHostInfo(
_In_ JsModuleRecord requestModule,
_In_ JsModuleHostInfoKind moduleHostInfo,
_Outptr_result_maybenull_ void** hostInfo);
///
/// Returns metadata relating to the exception that caused the runtime of the current context
/// to be in the exception state and resets the exception state for that runtime. The metadata
/// includes a reference to the exception itself.
///
///
///
/// If the runtime of the current context is not in an exception state, this API will return
/// JsErrorInvalidArgument. If the runtime is disabled, this will return an exception
/// indicating that the script was terminated, but it will not clear the exception (the
/// exception will be cleared if the runtime is re-enabled using
/// JsEnableRuntimeExecution).
///
///
/// The metadata value is a javascript object with the following properties: exception, the
/// thrown exception object; line, the 0 indexed line number where the exception was thrown;
/// column, the 0 indexed column number where the exception was thrown; length, the
/// source-length of the cause of the exception; source, a string containing the line of
/// source code where the exception was thrown; and url, a string containing the name of
/// the script file containing the code that threw the exception.
///
///
/// Requires an active script context.
///
///
/// The exception metadata for the runtime of the current context.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsGetAndClearExceptionWithMetadata(
_Out_ JsValueRef *metadata);
///
/// Called by the runtime to load the source code of the serialized script.
///
/// The context passed to Js[Parse|Run]SerializedScriptCallback
/// The script returned.
///
/// true if the operation succeeded, false otherwise.
///
typedef bool (CHAKRA_CALLBACK * JsSerializedLoadScriptCallback)
(JsSourceContext sourceContext, _Out_ JsValueRef *value,
_Out_ JsParseScriptAttributes *parseAttributes);
///
/// Create JavascriptString variable from ASCII or Utf8 string
///
///
///
/// Input string can be either ASCII or Utf8
///
///
/// Pointer to string memory.
/// Number of bytes within the string
/// JsValueRef representing the JavascriptString
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCreateString(
_In_ const char *content,
_In_ size_t length,
_Out_ JsValueRef *value);
///
/// Create JavascriptString variable from Utf16 string
///
///
///
/// Expects Utf16 string
///
///
/// Pointer to string memory.
/// Number of characters within the string
/// JsValueRef representing the JavascriptString
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCreateStringUtf16(
_In_ const uint16_t *content,
_In_ size_t length,
_Out_ JsValueRef *value);
///
/// Write JavascriptString value into C string buffer (Utf8)
///
///
///
/// When size of the `buffer` is unknown,
/// `buffer` argument can be nullptr.
/// In that case, `written` argument will return the length needed.
///
///
/// JavascriptString value
/// Pointer to buffer
/// Buffer size
/// Total number of characters written
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCopyString(
_In_ JsValueRef value,
_Out_opt_ char* buffer,
_In_ size_t bufferSize,
_Out_opt_ size_t* written);
///
/// Write string value into Utf16 string buffer
///
///
///
/// When size of the `buffer` is unknown,
/// `buffer` argument can be nullptr.
/// In that case, `written` argument will return the length needed.
///
///
/// when start is out of range or < 0, returns JsErrorInvalidArgument
/// and `written` will be equal to 0.
/// If calculated length is 0 (It can be due to string length or `start`
/// and length combination), then `written` will be equal to 0 and call
/// returns JsNoError
///
///
/// JavascriptString value
/// start offset of buffer
/// length to be written
/// Pointer to buffer
/// Total number of characters written
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCopyStringUtf16(
_In_ JsValueRef value,
_In_ int start,
_In_ int length,
_Out_opt_ uint16_t* buffer,
_Out_opt_ size_t* written);
///
/// Parses a script and returns a function representing the script.
///
///
///
/// Requires an active script context.
///
///
/// Script source can be either JavascriptString or JavascriptExternalArrayBuffer.
/// In case it is an ExternalArrayBuffer, and the encoding of the buffer is Utf16,
/// JsParseScriptAttributeArrayBufferIsUtf16Encoded is expected on parseAttributes.
///
///
/// Use JavascriptExternalArrayBuffer with Utf8/ASCII script source
/// for better performance and smaller memory footprint.
///
///
/// The script to run.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
///
/// The location the script came from.
/// Attribute mask for parsing the script
/// The result of the compiled script.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsParse(
_In_ JsValueRef script,
_In_ JsSourceContext sourceContext,
_In_ JsValueRef sourceUrl,
_In_ JsParseScriptAttributes parseAttributes,
_Out_ JsValueRef *result);
///
/// Executes a script.
///
///
///
/// Requires an active script context.
///
///
/// Script source can be either JavascriptString or JavascriptExternalArrayBuffer.
/// In case it is an ExternalArrayBuffer, and the encoding of the buffer is Utf16,
/// JsParseScriptAttributeArrayBufferIsUtf16Encoded is expected on parseAttributes.
///
///
/// Use JavascriptExternalArrayBuffer with Utf8/ASCII script source
/// for better performance and smaller memory footprint.
///
///
/// The script to run.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
///
/// The location the script came from
/// Attribute mask for parsing the script
/// The result of the script, if any. This parameter can be null.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsRun(
_In_ JsValueRef script,
_In_ JsSourceContext sourceContext,
_In_ JsValueRef sourceUrl,
_In_ JsParseScriptAttributes parseAttributes,
_Out_ JsValueRef *result);
///
/// Creates the property ID associated with the name.
///
///
///
/// Property IDs are specific to a context and cannot be used across contexts.
///
///
/// Requires an active script context.
///
///
///
/// The name of the property ID to get or create. The name may consist of only digits.
/// The string is expected to be ASCII / utf8 encoded.
///
/// length of the name in bytes
/// The property ID in this runtime for the given name.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCreatePropertyId(
_In_z_ const char *name,
_In_ size_t length,
_Out_ JsPropertyIdRef *propertyId);
///
/// Copies the name associated with the property ID into a buffer.
///
///
///
/// Requires an active script context.
///
///
/// When size of the `buffer` is unknown,
/// `buffer` argument can be nullptr.
/// `length` argument will return the size needed.
///
///
/// The property ID to get the name of.
/// The buffer holding the name associated with the property ID, encoded as utf8
/// Size of the buffer.
/// Total number of characters written or to be written
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCopyPropertyId(
_In_ JsPropertyIdRef propertyId,
_Out_ char* buffer,
_In_ size_t bufferSize,
_Out_ size_t* length);
///
/// Serializes a parsed script to a buffer than can be reused.
///
///
///
/// JsSerializeScript parses a script and then stores the parsed form of the script in a
/// runtime-independent format. The serialized script then can be deserialized in any
/// runtime without requiring the script to be re-parsed.
///
///
/// Requires an active script context.
///
///
/// Script source can be either JavascriptString or JavascriptExternalArrayBuffer.
/// In case it is an ExternalArrayBuffer, and the encoding of the buffer is Utf16,
/// JsParseScriptAttributeArrayBufferIsUtf16Encoded is expected on parseAttributes.
///
///
/// Use JavascriptExternalArrayBuffer with Utf8/ASCII script source
/// for better performance and smaller memory footprint.
///
///
/// The script to serialize
/// ArrayBuffer
/// Encoding for the script.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsSerialize(
_In_ JsValueRef script,
_Out_ JsValueRef *buffer,
_In_ JsParseScriptAttributes parseAttributes);
///
/// Parses a serialized script and returns a function representing the script.
/// Provides the ability to lazy load the script source only if/when it is needed.
///
///
///
/// Requires an active script context.
///
///
/// The serialized script as an ArrayBuffer (preferably ExternalArrayBuffer).
///
/// Callback called when the source code of the script needs to be loaded.
/// This is an optional parameter, set to null if not needed.
///
///
/// A cookie identifying the script that can be used by debuggable script contexts.
/// This context will passed into scriptLoadCallback.
///
/// The location the script came from.
/// A function representing the script code.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsParseSerialized(
_In_ JsValueRef buffer,
_In_ JsSerializedLoadScriptCallback scriptLoadCallback,
_In_ JsSourceContext sourceContext,
_In_ JsValueRef sourceUrl,
_Out_ JsValueRef *result);
///
/// Runs a serialized script.
/// Provides the ability to lazy load the script source only if/when it is needed.
///
///
///
/// Requires an active script context.
///
///
/// The runtime will hold on to the buffer until all instances of any functions created from
/// the buffer are garbage collected.
///
///
/// The serialized script as an ArrayBuffer (preferably ExternalArrayBuffer).
/// Callback called when the source code of the script needs to be loaded.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
/// This context will passed into scriptLoadCallback.
///
/// The location the script came from.
///
/// The result of running the script, if any. This parameter can be null.
///
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsRunSerialized(
_In_ JsValueRef buffer,
_In_ JsSerializedLoadScriptCallback scriptLoadCallback,
_In_ JsSourceContext sourceContext,
_In_ JsValueRef sourceUrl,
_Out_ JsValueRef *result);
///
/// Creates a new JavaScript Promise object.
///
///
/// Requires an active script context.
///
/// The new Promise object.
/// The function called to resolve the created Promise object.
/// The function called to reject the created Promise object.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCreatePromise(
_Out_ JsValueRef *promise,
_Out_ JsValueRef *resolveFunction,
_Out_ JsValueRef *rejectFunction);
///
/// A weak reference to a JavaScript value.
///
///
/// A value with only weak references is available for garbage-collection. A strong reference
/// to the value (JsValueRef) may be obtained from a weak reference if the value happens
/// to still be available.
///
typedef JsRef JsWeakRef;
///
/// Creates a weak reference to a value.
///
/// The value to be referenced.
/// Weak reference to the value.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCreateWeakReference(
_In_ JsValueRef value,
_Out_ JsWeakRef* weakRef);
///
/// Gets a strong reference to the value referred to by a weak reference.
///
/// A weak reference.
/// Reference to the value, or JS_INVALID_REFERENCE if the value is
/// no longer available.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsGetWeakReferenceValue(
_In_ JsWeakRef weakRef,
_Out_ JsValueRef* value);
///
/// Creates a Javascript SharedArrayBuffer object with shared content get from JsGetSharedArrayBufferContent.
///
///
/// Requires an active script context.
///
///
/// The storage object of a SharedArrayBuffer which can be shared between multiple thread.
///
/// The new SharedArrayBuffer object.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsCreateSharedArrayBufferWithSharedContent(
_In_ JsSharedArrayBufferContentHandle sharedContents,
_Out_ JsValueRef *result);
///
/// Get the storage object from a SharedArrayBuffer.
///
///
/// Requires an active script context.
///
/// The SharedArrayBuffer object.
///
/// The storage object of a SharedArrayBuffer which can be shared between multiple thread.
/// User should call JsReleaseSharedArrayBufferContentHandle after finished using it.
///
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsGetSharedArrayBufferContent(
_In_ JsValueRef sharedArrayBuffer,
_Out_ JsSharedArrayBufferContentHandle *sharedContents);
///
/// Decrease the reference count on a SharedArrayBuffer storage object.
///
///
/// Requires an active script context.
///
///
/// The storage object of a SharedArrayBuffer which can be shared between multiple thread.
///
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsReleaseSharedArrayBufferContentHandle(
_In_ JsSharedArrayBufferContentHandle sharedContents);
#endif // _CHAKRACOREBUILD
#endif // _CHAKRACORE_H_