ChakraCommonWindows.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #ifdef _MSC_VER
  6. #pragma once
  7. #endif // _MSC_VER
  8. #ifndef _CHAKRACOMMONWINDOWS_H_
  9. #define _CHAKRACOMMONWINDOWS_H_
  10. /// <summary>
  11. /// Called by the runtime to load the source code of the serialized script.
  12. /// The caller must keep the script buffer valid until the JsSerializedScriptUnloadCallback.
  13. /// This callback is only supported by the Win32 version of the API
  14. /// </summary>
  15. /// <param name="sourceContext">The context passed to Js[Parse|Run]SerializedScriptWithCallback</param>
  16. /// <param name="scriptBuffer">The script returned.</param>
  17. /// <returns>
  18. /// true if the operation succeeded, false otherwise.
  19. /// </returns>
  20. typedef bool (CHAKRA_CALLBACK * JsSerializedScriptLoadSourceCallback)(_In_ JsSourceContext sourceContext, _Outptr_result_z_ const WCHAR** scriptBuffer);
  21. /// <summary>
  22. /// Parses a script and returns a function representing the script.
  23. /// </summary>
  24. /// <remarks>
  25. /// Requires an active script context.
  26. /// </remarks>
  27. /// <param name="script">The script to parse.</param>
  28. /// <param name="sourceContext">
  29. /// A cookie identifying the script that can be used by debuggable script contexts.
  30. /// </param>
  31. /// <param name="sourceUrl">The location the script came from.</param>
  32. /// <param name="result">A function representing the script code.</param>
  33. /// <returns>
  34. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  35. /// </returns>
  36. CHAKRA_API
  37. JsParseScript(
  38. _In_z_ const wchar_t *script,
  39. _In_ JsSourceContext sourceContext,
  40. _In_z_ const wchar_t *sourceUrl,
  41. _Out_ JsValueRef *result);
  42. /// <summary>
  43. /// Parses a script and returns a function representing the script.
  44. /// </summary>
  45. /// <remarks>
  46. /// Requires an active script context.
  47. /// </remarks>
  48. /// <param name="script">The script to parse.</param>
  49. /// <param name="sourceContext">
  50. /// A cookie identifying the script that can be used by debuggable script contexts.
  51. /// </param>
  52. /// <param name="sourceUrl">The location the script came from.</param>
  53. /// <param name="parseAttributes">Attribute mask for parsing the script</param>
  54. /// <param name="result">A function representing the script code.</param>
  55. /// <returns>
  56. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  57. /// </returns>
  58. CHAKRA_API
  59. JsParseScriptWithAttributes(
  60. _In_z_ const wchar_t *script,
  61. _In_ JsSourceContext sourceContext,
  62. _In_z_ const wchar_t *sourceUrl,
  63. _In_ JsParseScriptAttributes parseAttributes,
  64. _Out_ JsValueRef *result);
  65. /// <summary>
  66. /// Executes a script.
  67. /// </summary>
  68. /// <remarks>
  69. /// Requires an active script context.
  70. /// </remarks>
  71. /// <param name="script">The script to run.</param>
  72. /// <param name="sourceContext">
  73. /// A cookie identifying the script that can be used by debuggable script contexts.
  74. /// </param>
  75. /// <param name="sourceUrl">The location the script came from.</param>
  76. /// <param name="result">The result of the script, if any. This parameter can be null.</param>
  77. /// <returns>
  78. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  79. /// </returns>
  80. CHAKRA_API
  81. JsRunScript(
  82. _In_z_ const wchar_t *script,
  83. _In_ JsSourceContext sourceContext,
  84. _In_z_ const wchar_t *sourceUrl,
  85. _Out_ JsValueRef *result);
  86. /// <summary>
  87. /// Executes a module.
  88. /// </summary>
  89. /// <remarks>
  90. /// Requires an active script context.
  91. /// </remarks>
  92. /// <param name="script">The module script to parse and execute.</param>
  93. /// <param name="sourceContext">
  94. /// A cookie identifying the script that can be used by debuggable script contexts.
  95. /// </param>
  96. /// <param name="sourceUrl">The location the module script came from.</param>
  97. /// <param name="result">The result of executing the module script, if any. This parameter can be null.</param>
  98. /// <returns>
  99. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  100. /// </returns>
  101. CHAKRA_API
  102. JsExperimentalApiRunModule(
  103. _In_z_ const wchar_t *script,
  104. _In_ JsSourceContext sourceContext,
  105. _In_z_ const wchar_t *sourceUrl,
  106. _Out_ JsValueRef *result);
  107. /// <summary>
  108. /// Serializes a parsed script to a buffer than can be reused.
  109. /// </summary>
  110. /// <remarks>
  111. /// <para>
  112. /// <c>JsSerializeScript</c> parses a script and then stores the parsed form of the script in a
  113. /// runtime-independent format. The serialized script then can be deserialized in any
  114. /// runtime without requiring the script to be re-parsed.
  115. /// </para>
  116. /// <para>
  117. /// Requires an active script context.
  118. /// </para>
  119. /// </remarks>
  120. /// <param name="script">The script to serialize.</param>
  121. /// <param name="buffer">The buffer to put the serialized script into. Can be null.</param>
  122. /// <param name="bufferSize">
  123. /// On entry, the size of the buffer, in bytes; on exit, the size of the buffer, in bytes,
  124. /// required to hold the serialized script.
  125. /// </param>
  126. /// <returns>
  127. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  128. /// </returns>
  129. CHAKRA_API
  130. JsSerializeScript(
  131. _In_z_ const wchar_t *script,
  132. _Out_writes_to_opt_(*bufferSize, *bufferSize) BYTE *buffer,
  133. _Inout_ unsigned int *bufferSize);
  134. /// <summary>
  135. /// Parses a serialized script and returns a function representing the script.
  136. /// Provides the ability to lazy load the script source only if/when it is needed.
  137. /// </summary>
  138. /// <remarks>
  139. /// <para>
  140. /// Requires an active script context.
  141. /// </para>
  142. /// <para>
  143. /// The runtime will hold on to the buffer until all instances of any functions created from
  144. /// the buffer are garbage collected. It will then call scriptUnloadCallback to inform the
  145. /// caller it is safe to release.
  146. /// </para>
  147. /// </remarks>
  148. /// <param name="scriptLoadCallback">Callback called when the source code of the script needs to be loaded.</param>
  149. /// <param name="scriptUnloadCallback">Callback called when the serialized script and source code are no longer needed.</param>
  150. /// <param name="buffer">The serialized script.</param>
  151. /// <param name="sourceContext">
  152. /// A cookie identifying the script that can be used by debuggable script contexts.
  153. /// This context will passed into scriptLoadCallback and scriptUnloadCallback.
  154. /// </param>
  155. /// <param name="sourceUrl">The location the script came from.</param>
  156. /// <param name="result">A function representing the script code.</param>
  157. /// <returns>
  158. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  159. /// </returns>
  160. CHAKRA_API
  161. JsParseSerializedScriptWithCallback(
  162. _In_ JsSerializedScriptLoadSourceCallback scriptLoadCallback,
  163. _In_ JsSerializedScriptUnloadCallback scriptUnloadCallback,
  164. _In_ BYTE *buffer,
  165. _In_ JsSourceContext sourceContext,
  166. _In_z_ const wchar_t *sourceUrl,
  167. _Out_ JsValueRef * result);
  168. /// <summary>
  169. /// Runs a serialized script.
  170. /// Provides the ability to lazy load the script source only if/when it is needed.
  171. /// </summary>
  172. /// <remarks>
  173. /// <para>
  174. /// Requires an active script context.
  175. /// </para>
  176. /// <para>
  177. /// The runtime will hold on to the buffer until all instances of any functions created from
  178. /// the buffer are garbage collected. It will then call scriptUnloadCallback to inform the
  179. /// caller it is safe to release.
  180. /// </para>
  181. /// </remarks>
  182. /// <param name="scriptLoadCallback">Callback called when the source code of the script needs to be loaded.</param>
  183. /// <param name="scriptUnloadCallback">Callback called when the serialized script and source code are no longer needed.</param>
  184. /// <param name="buffer">The serialized script.</param>
  185. /// <param name="sourceContext">
  186. /// A cookie identifying the script that can be used by debuggable script contexts.
  187. /// This context will passed into scriptLoadCallback and scriptUnloadCallback.
  188. /// </param>
  189. /// <param name="sourceUrl">The location the script came from.</param>
  190. /// <param name="result">
  191. /// The result of running the script, if any. This parameter can be null.
  192. /// </param>
  193. /// <returns>
  194. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  195. /// </returns>
  196. CHAKRA_API
  197. JsRunSerializedScriptWithCallback(
  198. _In_ JsSerializedScriptLoadSourceCallback scriptLoadCallback,
  199. _In_ JsSerializedScriptUnloadCallback scriptUnloadCallback,
  200. _In_ BYTE *buffer,
  201. _In_ JsSourceContext sourceContext,
  202. _In_z_ const wchar_t *sourceUrl,
  203. _Out_opt_ JsValueRef * result);
  204. /// <summary>
  205. /// Parses a serialized script and returns a function representing the script.
  206. /// </summary>
  207. /// <remarks>
  208. /// <para>
  209. /// Requires an active script context.
  210. /// </para>
  211. /// <para>
  212. /// The runtime will hold on to the buffer until all instances of any functions created from
  213. /// the buffer are garbage collected.
  214. /// </para>
  215. /// </remarks>
  216. /// <param name="script">The script to parse.</param>
  217. /// <param name="buffer">The serialized script.</param>
  218. /// <param name="sourceContext">
  219. /// A cookie identifying the script that can be used by debuggable script contexts.
  220. /// </param>
  221. /// <param name="sourceUrl">The location the script came from.</param>
  222. /// <param name="result">A function representing the script code.</param>
  223. /// <returns>
  224. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  225. /// </returns>
  226. CHAKRA_API
  227. JsParseSerializedScript(
  228. _In_z_ const wchar_t *script,
  229. _In_ BYTE *buffer,
  230. _In_ JsSourceContext sourceContext,
  231. _In_z_ const wchar_t *sourceUrl,
  232. _Out_ JsValueRef *result);
  233. /// <summary>
  234. /// Runs a serialized script.
  235. /// </summary>
  236. /// <remarks>
  237. /// <para>
  238. /// Requires an active script context.
  239. /// </para>
  240. /// <para>
  241. /// The runtime will hold on to the buffer until all instances of any functions created from
  242. /// the buffer are garbage collected.
  243. /// </para>
  244. /// </remarks>
  245. /// <param name="script">The source code of the serialized script.</param>
  246. /// <param name="buffer">The serialized script.</param>
  247. /// <param name="sourceContext">
  248. /// A cookie identifying the script that can be used by debuggable script contexts.
  249. /// </param>
  250. /// <param name="sourceUrl">The location the script came from.</param>
  251. /// <param name="result">
  252. /// The result of running the script, if any. This parameter can be null.
  253. /// </param>
  254. /// <returns>
  255. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  256. /// </returns>
  257. CHAKRA_API
  258. JsRunSerializedScript(
  259. _In_z_ const wchar_t *script,
  260. _In_ BYTE *buffer,
  261. _In_ JsSourceContext sourceContext,
  262. _In_z_ const wchar_t *sourceUrl,
  263. _Out_ JsValueRef *result);
  264. /// <summary>
  265. /// Gets the property ID associated with the name.
  266. /// </summary>
  267. /// <remarks>
  268. /// <para>
  269. /// Property IDs are specific to a context and cannot be used across contexts.
  270. /// </para>
  271. /// <para>
  272. /// Requires an active script context.
  273. /// </para>
  274. /// </remarks>
  275. /// <param name="name">
  276. /// The name of the property ID to get or create. The name may consist of only digits.
  277. /// </param>
  278. /// <param name="propertyId">The property ID in this runtime for the given name.</param>
  279. /// <returns>
  280. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  281. /// </returns>
  282. CHAKRA_API
  283. JsGetPropertyIdFromName(
  284. _In_z_ const wchar_t *name,
  285. _Out_ JsPropertyIdRef *propertyId);
  286. /// <summary>
  287. /// Gets the name associated with the property ID.
  288. /// </summary>
  289. /// <remarks>
  290. /// <para>
  291. /// Requires an active script context.
  292. /// </para>
  293. /// <para>
  294. /// The returned buffer is valid as long as the runtime is alive and cannot be used
  295. /// once the runtime has been disposed.
  296. /// </para>
  297. /// </remarks>
  298. /// <param name="propertyId">The property ID to get the name of.</param>
  299. /// <param name="name">The name associated with the property ID.</param>
  300. /// <returns>
  301. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  302. /// </returns>
  303. CHAKRA_API
  304. JsGetPropertyNameFromId(
  305. _In_ JsPropertyIdRef propertyId,
  306. _Outptr_result_z_ const wchar_t **name);
  307. /// <summary>
  308. /// Creates a string value from a string pointer.
  309. /// </summary>
  310. /// <remarks>
  311. /// Requires an active script context.
  312. /// </remarks>
  313. /// <param name="stringValue">The string pointer to convert to a string value.</param>
  314. /// <param name="stringLength">The length of the string to convert.</param>
  315. /// <param name="value">The new string value.</param>
  316. /// <returns>
  317. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  318. /// </returns>
  319. CHAKRA_API
  320. JsPointerToString(
  321. _In_reads_(stringLength) const wchar_t *stringValue,
  322. _In_ size_t stringLength,
  323. _Out_ JsValueRef *value);
  324. /// <summary>
  325. /// Retrieves the string pointer of a string value.
  326. /// </summary>
  327. /// <remarks>
  328. /// <para>
  329. /// This function retrieves the string pointer of a string value. It will fail with
  330. /// <c>JsErrorInvalidArgument</c> if the type of the value is not string. The lifetime
  331. /// of the string returned will be the same as the lifetime of the value it came from, however
  332. /// the string pointer is not considered a reference to the value (and so will not keep it
  333. /// from being collected).
  334. /// </para>
  335. /// <para>
  336. /// Requires an active script context.
  337. /// </para>
  338. /// </remarks>
  339. /// <param name="value">The string value to convert to a string pointer.</param>
  340. /// <param name="stringValue">The string pointer.</param>
  341. /// <param name="stringLength">The length of the string.</param>
  342. /// <returns>
  343. /// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
  344. /// </returns>
  345. CHAKRA_API
  346. JsStringToPointer(
  347. _In_ JsValueRef value,
  348. _Outptr_result_buffer_(*stringLength) const wchar_t **stringValue,
  349. _Out_ size_t *stringLength);
  350. #endif // _CHAKRACOMMONWINDOWS_H_