CefV8Value.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class representing a V8 value handle. V8 handles can only be accessed from
  10. /// the thread on which they are created. Valid threads for creating a V8 handle
  11. /// include the render process main thread (TID_RENDERER) and WebWorker threads.
  12. /// A task runner for posting tasks on the associated thread can be retrieved via
  13. /// the CefV8Context::GetTaskRunner() method.
  14. /// </summary>
  15. public sealed unsafe partial class CefV8Value
  16. {
  17. /// <summary>
  18. /// Create a new CefV8Value object of type undefined.
  19. /// </summary>
  20. public static CefV8Value CreateUndefined()
  21. {
  22. return CefV8Value.FromNative(
  23. cef_v8value_t.create_undefined()
  24. );
  25. }
  26. /// <summary>
  27. /// Create a new CefV8Value object of type null.
  28. /// </summary>
  29. public static CefV8Value CreateNull()
  30. {
  31. return CefV8Value.FromNative(
  32. cef_v8value_t.create_null()
  33. );
  34. }
  35. /// <summary>
  36. /// Create a new CefV8Value object of type bool.
  37. /// </summary>
  38. public static CefV8Value CreateBool(bool value)
  39. {
  40. return CefV8Value.FromNative(
  41. cef_v8value_t.create_bool(value ? 1 : 0)
  42. );
  43. }
  44. /// <summary>
  45. /// Create a new CefV8Value object of type int.
  46. /// </summary>
  47. public static CefV8Value CreateInt(int value)
  48. {
  49. return CefV8Value.FromNative(
  50. cef_v8value_t.create_int(value)
  51. );
  52. }
  53. /// <summary>
  54. /// Create a new CefV8Value object of type unsigned int.
  55. /// </summary>
  56. public static CefV8Value CreateUInt(uint value)
  57. {
  58. return CefV8Value.FromNative(
  59. cef_v8value_t.create_uint(value)
  60. );
  61. }
  62. /// <summary>
  63. /// Create a new CefV8Value object of type double.
  64. /// </summary>
  65. public static CefV8Value CreateDouble(double value)
  66. {
  67. return CefV8Value.FromNative(
  68. cef_v8value_t.create_double(value)
  69. );
  70. }
  71. /// <summary>
  72. /// Create a new CefV8Value object of type Date. This method should only be
  73. /// called from within the scope of a CefRenderProcessHandler, CefV8Handler or
  74. /// CefV8Accessor callback, or in combination with calling Enter() and Exit()
  75. /// on a stored CefV8Context reference.
  76. /// </summary>
  77. public static CefV8Value CreateDate(DateTime value)
  78. {
  79. var n_value = new cef_time_t(value);
  80. return CefV8Value.FromNative(
  81. cef_v8value_t.create_date(&n_value)
  82. );
  83. }
  84. /// <summary>
  85. /// Create a new CefV8Value object of type string.
  86. /// </summary>
  87. public static CefV8Value CreateString(string value)
  88. {
  89. fixed (char* value_str = value)
  90. {
  91. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  92. return CefV8Value.FromNative(
  93. cef_v8value_t.create_string(&n_value)
  94. );
  95. }
  96. }
  97. /// <summary>
  98. /// Create a new CefV8Value object of type object with optional accessor and/or
  99. /// interceptor. This method should only be called from within the scope of a
  100. /// CefRenderProcessHandler, CefV8Handler or CefV8Accessor callback, or in
  101. /// combination with calling Enter() and Exit() on a stored CefV8Context
  102. /// reference.
  103. /// </summary>
  104. public static CefV8Value CreateObject(CefV8Accessor accessor = null, CefV8Interceptor interceptor = null)
  105. {
  106. return CefV8Value.FromNative(
  107. cef_v8value_t.create_object(
  108. accessor != null ? accessor.ToNative() : null,
  109. interceptor != null ? interceptor.ToNative() : null
  110. )
  111. );
  112. }
  113. /// <summary>
  114. /// Create a new CefV8Value object of type array with the specified |length|.
  115. /// If |length| is negative the returned array will have length 0. This method
  116. /// should only be called from within the scope of a CefRenderProcessHandler,
  117. /// CefV8Handler or CefV8Accessor callback, or in combination with calling
  118. /// Enter() and Exit() on a stored CefV8Context reference.
  119. /// </summary>
  120. public static CefV8Value CreateArray(int length)
  121. {
  122. return CefV8Value.FromNative(
  123. cef_v8value_t.create_array(length)
  124. );
  125. }
  126. /// <summary>
  127. /// Create a new CefV8Value object of type ArrayBuffer which wraps the provided
  128. /// |buffer| of size |length| bytes. The ArrayBuffer is externalized, meaning
  129. /// that it does not own |buffer|. The caller is responsible for freeing
  130. /// |buffer| when requested via a call to CefV8ArrayBufferReleaseCallback::
  131. /// ReleaseBuffer. This method should only be called from within the scope of a
  132. /// CefRenderProcessHandler, CefV8Handler or CefV8Accessor callback, or in
  133. /// combination with calling Enter() and Exit() on a stored CefV8Context
  134. /// reference.
  135. /// </summary>
  136. public static CefV8Value CreateArrayBuffer(IntPtr buffer, ulong length, CefV8ArrayBufferReleaseCallback releaseCallback)
  137. {
  138. if (releaseCallback == null) throw new ArgumentNullException(nameof(releaseCallback));
  139. var n_value = cef_v8value_t.create_array_buffer(
  140. (void*)buffer,
  141. checked((UIntPtr)length),
  142. releaseCallback.ToNative()
  143. );
  144. return CefV8Value.FromNative(n_value);
  145. }
  146. /// <summary>
  147. /// Create a new CefV8Value object of type function. This method should only be
  148. /// called from within the scope of a CefRenderProcessHandler, CefV8Handler or
  149. /// CefV8Accessor callback, or in combination with calling Enter() and Exit()
  150. /// on a stored CefV8Context reference.
  151. /// </summary>
  152. public static CefV8Value CreateFunction(string name, CefV8Handler handler)
  153. {
  154. fixed (char* name_str = name)
  155. {
  156. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  157. return CefV8Value.FromNative(
  158. cef_v8value_t.create_function(&n_name, handler.ToNative())
  159. );
  160. }
  161. }
  162. /// <summary>
  163. /// Returns true if the underlying handle is valid and it can be accessed on
  164. /// the current thread. Do not call any other methods if this method returns
  165. /// false.
  166. /// </summary>
  167. public bool IsValid
  168. {
  169. get { return cef_v8value_t.is_valid(_self) != 0; }
  170. }
  171. /// <summary>
  172. /// True if the value type is undefined.
  173. /// </summary>
  174. public bool IsUndefined
  175. {
  176. get { return cef_v8value_t.is_undefined(_self) != 0; }
  177. }
  178. /// <summary>
  179. /// True if the value type is null.
  180. /// </summary>
  181. public bool IsNull
  182. {
  183. get { return cef_v8value_t.is_null(_self) != 0; }
  184. }
  185. /// <summary>
  186. /// True if the value type is bool.
  187. /// </summary>
  188. public bool IsBool
  189. {
  190. get { return cef_v8value_t.is_bool(_self) != 0; }
  191. }
  192. /// <summary>
  193. /// True if the value type is int.
  194. /// </summary>
  195. public bool IsInt
  196. {
  197. get { return cef_v8value_t.is_int(_self) != 0; }
  198. }
  199. /// <summary>
  200. /// True if the value type is unsigned int.
  201. /// </summary>
  202. public bool IsUInt
  203. {
  204. get { return cef_v8value_t.is_uint(_self) != 0; }
  205. }
  206. /// <summary>
  207. /// True if the value type is double.
  208. /// </summary>
  209. public bool IsDouble
  210. {
  211. get { return cef_v8value_t.is_double(_self) != 0; }
  212. }
  213. /// <summary>
  214. /// True if the value type is Date.
  215. /// </summary>
  216. public bool IsDate
  217. {
  218. get { return cef_v8value_t.is_date(_self) != 0; }
  219. }
  220. /// <summary>
  221. /// True if the value type is string.
  222. /// </summary>
  223. public bool IsString
  224. {
  225. get { return cef_v8value_t.is_string(_self) != 0; }
  226. }
  227. /// <summary>
  228. /// True if the value type is object.
  229. /// </summary>
  230. public bool IsObject
  231. {
  232. get { return cef_v8value_t.is_object(_self) != 0; }
  233. }
  234. /// <summary>
  235. /// True if the value type is array.
  236. /// </summary>
  237. public bool IsArray
  238. {
  239. get { return cef_v8value_t.is_array(_self) != 0; }
  240. }
  241. /// <summary>
  242. /// True if the value type is an ArrayBuffer.
  243. /// </summary>
  244. public bool IsArrayBuffer
  245. {
  246. get { return cef_v8value_t.is_array_buffer(_self) != 0; }
  247. }
  248. /// <summary>
  249. /// True if the value type is function.
  250. /// </summary>
  251. public bool IsFunction
  252. {
  253. get { return cef_v8value_t.is_function(_self) != 0; }
  254. }
  255. /// <summary>
  256. /// Returns true if this object is pointing to the same handle as |that|
  257. /// object.
  258. /// </summary>
  259. public bool IsSame(CefV8Value that)
  260. {
  261. if (that == null) return false;
  262. return cef_v8value_t.is_same(_self, that.ToNative()) != 0;
  263. }
  264. /// <summary>
  265. /// Return a bool value.
  266. /// </summary>
  267. public bool GetBoolValue()
  268. {
  269. return cef_v8value_t.get_bool_value(_self) != 0;
  270. }
  271. /// <summary>
  272. /// Return an int value.
  273. /// </summary>
  274. public int GetIntValue()
  275. {
  276. return cef_v8value_t.get_int_value(_self);
  277. }
  278. /// <summary>
  279. /// Return an unsigned int value.
  280. /// </summary>
  281. public uint GetUIntValue()
  282. {
  283. return cef_v8value_t.get_uint_value(_self);
  284. }
  285. /// <summary>
  286. /// Return a double value.
  287. /// </summary>
  288. public double GetDoubleValue()
  289. {
  290. return cef_v8value_t.get_double_value(_self);
  291. }
  292. /// <summary>
  293. /// Return a Date value.
  294. /// </summary>
  295. public DateTime GetDateValue()
  296. {
  297. var value = cef_v8value_t.get_date_value(_self);
  298. return cef_time_t.ToDateTime(&value);
  299. }
  300. /// <summary>
  301. /// Return a string value.
  302. /// </summary>
  303. public string GetStringValue()
  304. {
  305. var n_result = cef_v8value_t.get_string_value(_self);
  306. return cef_string_userfree.ToString(n_result);
  307. }
  308. /// <summary>
  309. /// OBJECT METHODS - These methods are only available on objects. Arrays and
  310. /// functions are also objects. String- and integer-based keys can be used
  311. /// interchangably with the framework converting between them as necessary.
  312. /// Returns true if this is a user created object.
  313. /// </summary>
  314. public bool IsUserCreated
  315. {
  316. get { return cef_v8value_t.is_user_created(_self) != 0; }
  317. }
  318. /// <summary>
  319. /// Returns true if the last method call resulted in an exception. This
  320. /// attribute exists only in the scope of the current CEF value object.
  321. /// </summary>
  322. public bool HasException
  323. {
  324. get { return cef_v8value_t.has_exception(_self) != 0; }
  325. }
  326. /// <summary>
  327. /// Returns the exception resulting from the last method call. This attribute
  328. /// exists only in the scope of the current CEF value object.
  329. /// </summary>
  330. public CefV8Exception GetException()
  331. {
  332. return CefV8Exception.FromNativeOrNull(
  333. cef_v8value_t.get_exception(_self)
  334. );
  335. }
  336. /// <summary>
  337. /// Clears the last exception and returns true on success.
  338. /// </summary>
  339. public bool ClearException()
  340. {
  341. return cef_v8value_t.clear_exception(_self) != 0;
  342. }
  343. /// <summary>
  344. /// Returns true if this object will re-throw future exceptions. This attribute
  345. /// exists only in the scope of the current CEF value object.
  346. /// </summary>
  347. public bool WillRethrowExceptions()
  348. {
  349. return cef_v8value_t.will_rethrow_exceptions(_self) != 0;
  350. }
  351. /// <summary>
  352. /// Set whether this object will re-throw future exceptions. By default
  353. /// exceptions are not re-thrown. If a exception is re-thrown the current
  354. /// context should not be accessed again until after the exception has been
  355. /// caught and not re-thrown. Returns true on success. This attribute exists
  356. /// only in the scope of the current CEF value object.
  357. /// </summary>
  358. public bool SetRethrowExceptions(bool rethrow)
  359. {
  360. return cef_v8value_t.set_rethrow_exceptions(_self, rethrow ? 1 : 0) != 0;
  361. }
  362. /// <summary>
  363. /// Returns true if the object has a value with the specified identifier.
  364. /// </summary>
  365. public bool HasValue(string key)
  366. {
  367. fixed (char* key_str = key)
  368. {
  369. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  370. return cef_v8value_t.has_value_bykey(_self, &n_key) != 0;
  371. }
  372. }
  373. /// <summary>
  374. /// Returns true if the object has a value with the specified identifier.
  375. /// </summary>
  376. public bool HasValue(int index)
  377. {
  378. return cef_v8value_t.has_value_byindex(_self, index) != 0;
  379. }
  380. /// <summary>
  381. /// Deletes the value with the specified identifier and returns true on
  382. /// success. Returns false if this method is called incorrectly or an exception
  383. /// is thrown. For read-only and don't-delete values this method will return
  384. /// true even though deletion failed.
  385. /// </summary>
  386. public bool DeleteValue(string key)
  387. {
  388. fixed (char* key_str = key)
  389. {
  390. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  391. return cef_v8value_t.delete_value_bykey(_self, &n_key) != 0;
  392. }
  393. }
  394. /// <summary>
  395. /// Deletes the value with the specified identifier and returns true on
  396. /// success. Returns false if this method is called incorrectly, deletion fails
  397. /// or an exception is thrown. For read-only and don't-delete values this
  398. /// method will return true even though deletion failed.
  399. /// </summary>
  400. public bool DeleteValue(int index)
  401. {
  402. return cef_v8value_t.delete_value_byindex(_self, index) != 0;
  403. }
  404. /// <summary>
  405. /// Returns the value with the specified identifier on success. Returns NULL
  406. /// if this method is called incorrectly or an exception is thrown.
  407. /// </summary>
  408. public CefV8Value GetValue(string key)
  409. {
  410. fixed (char* key_str = key)
  411. {
  412. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  413. return CefV8Value.FromNativeOrNull(
  414. cef_v8value_t.get_value_bykey(_self, &n_key)
  415. );
  416. }
  417. }
  418. /// <summary>
  419. /// Returns the value with the specified identifier on success. Returns NULL
  420. /// if this method is called incorrectly or an exception is thrown.
  421. /// </summary>
  422. public CefV8Value GetValue(int index)
  423. {
  424. return CefV8Value.FromNativeOrNull(
  425. cef_v8value_t.get_value_byindex(_self, index)
  426. );
  427. }
  428. /// <summary>
  429. /// Associates a value with the specified identifier and returns true on
  430. /// success. Returns false if this method is called incorrectly or an exception
  431. /// is thrown. For read-only values this method will return true even though
  432. /// assignment failed.
  433. /// </summary>
  434. public bool SetValue(string key, CefV8Value value, CefV8PropertyAttribute attribute = CefV8PropertyAttribute.None)
  435. {
  436. fixed (char* key_str = key)
  437. {
  438. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  439. return cef_v8value_t.set_value_bykey(_self, &n_key, value.ToNative(), attribute) != 0;
  440. }
  441. }
  442. /// <summary>
  443. /// Associates a value with the specified identifier and returns true on
  444. /// success. Returns false if this method is called incorrectly or an exception
  445. /// is thrown. For read-only values this method will return true even though
  446. /// assignment failed.
  447. /// </summary>
  448. public bool SetValue(int index, CefV8Value value)
  449. {
  450. return cef_v8value_t.set_value_byindex(_self, index, value.ToNative()) != 0;
  451. }
  452. /// <summary>
  453. /// Registers an identifier and returns true on success. Access to the
  454. /// identifier will be forwarded to the CefV8Accessor instance passed to
  455. /// CefV8Value::CreateObject(). Returns false if this method is called
  456. /// incorrectly or an exception is thrown. For read-only values this method
  457. /// will return true even though assignment failed.
  458. /// </summary>
  459. public bool SetValue(string key, CefV8AccessControl settings, CefV8PropertyAttribute attribute = CefV8PropertyAttribute.None)
  460. {
  461. fixed (char* key_str = key)
  462. {
  463. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  464. return cef_v8value_t.set_value_byaccessor(_self, &n_key, settings, attribute) != 0;
  465. }
  466. }
  467. /// <summary>
  468. /// Read the keys for the object's values into the specified vector. Integer-
  469. /// based keys will also be returned as strings.
  470. /// </summary>
  471. public bool TryGetKeys(out string[] keys)
  472. {
  473. var list = libcef.string_list_alloc();
  474. var result = cef_v8value_t.get_keys(_self, list) != 0;
  475. if (result) keys = cef_string_list.ToArray(list);
  476. else keys = null;
  477. libcef.string_list_free(list);
  478. return result;
  479. }
  480. /// <summary>
  481. /// Read the keys for the object's values into the specified vector. Integer-
  482. /// based keys will also be returned as strings.
  483. /// </summary>
  484. public string[] GetKeys()
  485. {
  486. string[] keys;
  487. if (TryGetKeys(out keys)) return keys;
  488. else throw new InvalidOperationException();
  489. }
  490. /// <summary>
  491. /// Sets the user data for this object and returns true on success. Returns
  492. /// false if this method is called incorrectly. This method can only be called
  493. /// on user created objects.
  494. /// </summary>
  495. public bool SetUserData(CefUserData userData)
  496. {
  497. return cef_v8value_t.set_user_data(_self, userData != null ? (cef_base_ref_counted_t*)userData.ToNative() : null) != 0;
  498. }
  499. /// <summary>
  500. /// Returns the user data, if any, assigned to this object.
  501. /// </summary>
  502. public CefUserData GetUserData()
  503. {
  504. return CefUserData.FromNativeOrNull(
  505. (cef_user_data_t*)cef_v8value_t.get_user_data(_self)
  506. );
  507. }
  508. /// <summary>
  509. /// Returns the amount of externally allocated memory registered for the
  510. /// object.
  511. /// </summary>
  512. public int GetExternallyAllocatedMemory()
  513. {
  514. return cef_v8value_t.get_externally_allocated_memory(_self);
  515. }
  516. /// <summary>
  517. /// Adjusts the amount of registered external memory for the object. Used to
  518. /// give V8 an indication of the amount of externally allocated memory that is
  519. /// kept alive by JavaScript objects. V8 uses this information to decide when
  520. /// to perform global garbage collection. Each CefV8Value tracks the amount of
  521. /// external memory associated with it and automatically decreases the global
  522. /// total by the appropriate amount on its destruction. |change_in_bytes|
  523. /// specifies the number of bytes to adjust by. This method returns the number
  524. /// of bytes associated with the object after the adjustment. This method can
  525. /// only be called on user created objects.
  526. /// </summary>
  527. public int AdjustExternallyAllocatedMemory(int changeInBytes)
  528. {
  529. return cef_v8value_t.adjust_externally_allocated_memory(_self, changeInBytes);
  530. }
  531. /// <summary>
  532. /// ARRAY METHODS - These methods are only available on arrays.
  533. /// Returns the number of elements in the array.
  534. /// </summary>
  535. public int GetArrayLength()
  536. {
  537. return cef_v8value_t.get_array_length(_self);
  538. }
  539. /// <summary>
  540. /// ARRAY BUFFER METHODS - These methods are only available on ArrayBuffers.
  541. /// Returns the ReleaseCallback object associated with the ArrayBuffer or NULL
  542. /// if the ArrayBuffer was not created with CreateArrayBuffer.
  543. /// </summary>
  544. public CefV8ArrayBufferReleaseCallback GetArrayBufferReleaseCallback()
  545. {
  546. var n_releaseCallback = cef_v8value_t.get_array_buffer_release_callback(_self);
  547. return CefV8ArrayBufferReleaseCallback.FromNativeOrNull(n_releaseCallback);
  548. }
  549. /// <summary>
  550. /// Prevent the ArrayBuffer from using it's memory block by setting the length
  551. /// to zero. This operation cannot be undone. If the ArrayBuffer was created
  552. /// with CreateArrayBuffer then CefV8ArrayBufferReleaseCallback::ReleaseBuffer
  553. /// will be called to release the underlying buffer.
  554. /// </summary>
  555. public bool NeuterArrayBuffer()
  556. {
  557. return cef_v8value_t.neuter_array_buffer(_self) != 0;
  558. }
  559. /// <summary>
  560. /// FUNCTION METHODS - These methods are only available on functions.
  561. /// Returns the function name.
  562. /// </summary>
  563. public string GetFunctionName()
  564. {
  565. var n_result = cef_v8value_t.get_function_name(_self);
  566. return cef_string_userfree.ToString(n_result);
  567. }
  568. /// <summary>
  569. /// Returns the function handler or NULL if not a CEF-created function.
  570. /// </summary>
  571. public CefV8Handler GetFunctionHandler()
  572. {
  573. return CefV8Handler.FromNativeOrNull(
  574. cef_v8value_t.get_function_handler(_self)
  575. );
  576. }
  577. /// <summary>
  578. /// Execute the function using the current V8 context. This method should only
  579. /// be called from within the scope of a CefV8Handler or CefV8Accessor
  580. /// callback, or in combination with calling Enter() and Exit() on a stored
  581. /// CefV8Context reference. |object| is the receiver ('this' object) of the
  582. /// function. If |object| is empty the current context's global object will be
  583. /// used. |arguments| is the list of arguments that will be passed to the
  584. /// function. Returns the function return value on success. Returns NULL if
  585. /// this method is called incorrectly or an exception is thrown.
  586. /// </summary>
  587. public CefV8Value ExecuteFunction(CefV8Value obj, CefV8Value[] arguments)
  588. {
  589. var n_arguments = CreateArguments(arguments);
  590. cef_v8value_t* n_retval;
  591. fixed (cef_v8value_t** n_arguments_ptr = n_arguments)
  592. {
  593. n_retval = cef_v8value_t.execute_function(
  594. _self,
  595. obj != null ? obj.ToNative() : null,
  596. n_arguments != null ? (UIntPtr)n_arguments.Length : UIntPtr.Zero,
  597. n_arguments_ptr
  598. );
  599. }
  600. return CefV8Value.FromNativeOrNull(n_retval);
  601. }
  602. /// <summary>
  603. /// Execute the function using the specified V8 context. |object| is the
  604. /// receiver ('this' object) of the function. If |object| is empty the
  605. /// specified context's global object will be used. |arguments| is the list of
  606. /// arguments that will be passed to the function. Returns the function return
  607. /// value on success. Returns NULL if this method is called incorrectly or an
  608. /// exception is thrown.
  609. /// </summary>
  610. public CefV8Value ExecuteFunctionWithContext(CefV8Context context, CefV8Value obj, CefV8Value[] arguments)
  611. {
  612. var n_arguments = CreateArguments(arguments);
  613. cef_v8value_t* n_retval;
  614. fixed (cef_v8value_t** n_arguments_ptr = n_arguments)
  615. {
  616. n_retval = cef_v8value_t.execute_function_with_context(
  617. _self,
  618. context.ToNative(),
  619. obj != null ? obj.ToNative() : null,
  620. n_arguments != null ? (UIntPtr)n_arguments.Length : UIntPtr.Zero,
  621. n_arguments_ptr
  622. );
  623. }
  624. return CefV8Value.FromNativeOrNull(n_retval);
  625. }
  626. private static cef_v8value_t*[] CreateArguments(CefV8Value[] arguments)
  627. {
  628. if (arguments == null) return null;
  629. var length = arguments.Length;
  630. if (length == 0) return null;
  631. var result = new cef_v8value_t*[arguments.Length];
  632. for (var i = 0; i < length; i++)
  633. {
  634. result[i] = arguments[i].ToNative();
  635. }
  636. return result;
  637. }
  638. }
  639. }