JavaScriptObject.xml 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="JavaScriptObject" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
  3. <brief_description>
  4. A wrapper class for web native JavaScript objects.
  5. </brief_description>
  6. <description>
  7. JavaScriptObject is used to interact with JavaScript objects retrieved or created via [method JavaScriptBridge.get_interface], [method JavaScriptBridge.create_object], or [method JavaScriptBridge.create_callback].
  8. [b]Example:[/b]
  9. [codeblock]
  10. extends Node
  11. var _my_js_callback = JavaScriptBridge.create_callback(self, "myCallback") # This reference must be kept
  12. var console = JavaScriptBridge.get_interface("console")
  13. func _init():
  14. var buf = JavaScriptBridge.create_object("ArrayBuffer", 10) # new ArrayBuffer(10)
  15. print(buf) # prints [JavaScriptObject:OBJECT_ID]
  16. var uint8arr = JavaScriptBridge.create_object("Uint8Array", buf) # new Uint8Array(buf)
  17. uint8arr[1] = 255
  18. prints(uint8arr[1], uint8arr.byteLength) # prints 255 10
  19. console.log(uint8arr) # prints in browser console "Uint8Array(10) [ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 ]"
  20. # Equivalent of JavaScriptBridge: Array.from(uint8arr).forEach(myCallback)
  21. JavaScriptBridge.get_interface("Array").from(uint8arr).forEach(_my_js_callback)
  22. func myCallback(args):
  23. # Will be called with the parameters passed to the "forEach" callback
  24. # [0, 0, [JavaScriptObject:1173]]
  25. # [255, 1, [JavaScriptObject:1173]]
  26. # ...
  27. # [0, 9, [JavaScriptObject:1180]]
  28. print(args)
  29. [/codeblock]
  30. [b]Note:[/b] Only available in the Web platform.
  31. </description>
  32. <tutorials>
  33. </tutorials>
  34. </class>