//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
#pragma once
#endif // _MSC_VER
#ifndef _CHAKRACOMMONWINDOWS_H_
#define _CHAKRACOMMONWINDOWS_H_
///
/// 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 context passed to Js[Parse|Run]SerializedScriptWithCallback
/// The script returned.
///
/// true if the operation succeeded, false otherwise.
///
typedef bool (CHAKRA_CALLBACK * JsSerializedScriptLoadSourceCallback)(_In_ JsSourceContext sourceContext, _Outptr_result_z_ const WCHAR** scriptBuffer);
///
/// Parses a script and returns a function representing the script.
///
///
/// Requires an active script context.
///
/// The script to parse.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
///
/// 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
JsParseScript(
_In_z_ const wchar_t *script,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *sourceUrl,
_Out_ JsValueRef *result);
///
/// Parses a script and returns a function representing the script.
///
///
/// Requires an active script context.
///
/// The script to parse.
///
/// 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
/// A function representing the script code.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsParseScriptWithAttributes(
_In_z_ const wchar_t *script,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *sourceUrl,
_In_ JsParseScriptAttributes parseAttributes,
_Out_ JsValueRef *result);
///
/// Executes a script.
///
///
/// Requires an active script context.
///
/// The script to run.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
///
/// The location the script came from.
/// 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
JsRunScript(
_In_z_ const wchar_t *script,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *sourceUrl,
_Out_ JsValueRef *result);
///
/// Executes a module.
///
///
/// Requires an active script context.
///
/// The module script to parse and execute.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
///
/// The location the module script came from.
/// The result of executing the module script, if any. This parameter can be null.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsExperimentalApiRunModule(
_In_z_ const wchar_t *script,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *sourceUrl,
_Out_ JsValueRef *result);
///
/// 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.
///
///
/// The script to serialize.
/// The buffer to put the serialized script into. Can be null.
///
/// On entry, the size of the buffer, in bytes; on exit, the size of the buffer, in bytes,
/// required to hold the serialized script.
///
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsSerializeScript(
_In_z_ const wchar_t *script,
_Out_writes_to_opt_(*bufferSize, *bufferSize) BYTE *buffer,
_Inout_ unsigned int *bufferSize);
///
/// 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 runtime will hold on to the buffer until all instances of any functions created from
/// the buffer are garbage collected. It will then call scriptUnloadCallback to inform the
/// caller it is safe to release.
///
///
/// Callback called when the source code of the script needs to be loaded.
/// Callback called when the serialized script and source code are no longer needed.
/// The serialized script.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
/// This context will passed into scriptLoadCallback and scriptUnloadCallback.
///
/// 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
JsParseSerializedScriptWithCallback(
_In_ JsSerializedScriptLoadSourceCallback scriptLoadCallback,
_In_ JsSerializedScriptUnloadCallback scriptUnloadCallback,
_In_ BYTE *buffer,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *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. It will then call scriptUnloadCallback to inform the
/// caller it is safe to release.
///
///
/// Callback called when the source code of the script needs to be loaded.
/// Callback called when the serialized script and source code are no longer needed.
/// The serialized script.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
/// This context will passed into scriptLoadCallback and scriptUnloadCallback.
///
/// 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
JsRunSerializedScriptWithCallback(
_In_ JsSerializedScriptLoadSourceCallback scriptLoadCallback,
_In_ JsSerializedScriptUnloadCallback scriptUnloadCallback,
_In_ BYTE *buffer,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *sourceUrl,
_Out_opt_ JsValueRef * result);
///
/// Parses a serialized script and returns a function representing the script.
///
///
///
/// 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 script to parse.
/// The serialized script.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
///
/// 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
JsParseSerializedScript(
_In_z_ const wchar_t *script,
_In_ BYTE *buffer,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *sourceUrl,
_Out_ JsValueRef *result);
///
/// Runs a serialized script.
///
///
///
/// 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 source code of the serialized script.
/// The serialized script.
///
/// A cookie identifying the script that can be used by debuggable script contexts.
///
/// 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
JsRunSerializedScript(
_In_z_ const wchar_t *script,
_In_ BYTE *buffer,
_In_ JsSourceContext sourceContext,
_In_z_ const wchar_t *sourceUrl,
_Out_ JsValueRef *result);
///
/// Gets 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 property ID in this runtime for the given name.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsGetPropertyIdFromName(
_In_z_ const wchar_t *name,
_Out_ JsPropertyIdRef *propertyId);
///
/// Gets the name associated with the property ID.
///
///
///
/// Requires an active script context.
///
///
/// The returned buffer is valid as long as the runtime is alive and cannot be used
/// once the runtime has been disposed.
///
///
/// The property ID to get the name of.
/// The name associated with the property ID.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsGetPropertyNameFromId(
_In_ JsPropertyIdRef propertyId,
_Outptr_result_z_ const wchar_t **name);
///
/// Creates a string value from a string pointer.
///
///
/// Requires an active script context.
///
/// The string pointer to convert to a string value.
/// The length of the string to convert.
/// The new string value.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsPointerToString(
_In_reads_(stringLength) const wchar_t *stringValue,
_In_ size_t stringLength,
_Out_ JsValueRef *value);
///
/// Retrieves the string pointer of a string value.
///
///
///
/// This function retrieves the string pointer of a string value. It will fail with
/// JsErrorInvalidArgument if the type of the value is not string. The lifetime
/// of the string returned will be the same as the lifetime of the value it came from, however
/// the string pointer is not considered a reference to the value (and so will not keep it
/// from being collected).
///
///
/// Requires an active script context.
///
///
/// The string value to convert to a string pointer.
/// The string pointer.
/// The length of the string.
///
/// The code JsNoError if the operation succeeded, a failure code otherwise.
///
CHAKRA_API
JsStringToPointer(
_In_ JsValueRef value,
_Outptr_result_buffer_(*stringLength) const wchar_t **stringValue,
_Out_ size_t *stringLength);
#endif // _CHAKRACOMMONWINDOWS_H_