From a82f70ea9f98ed0a602a20f7fe8954f6e4200b25 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Thu, 19 Nov 2020 16:54:07 +0100 Subject: [HTML5] Libraries refactor for linting. Initial work to make liniting easier. This includes: - Rename http_request.js to library_godot_http_request.js. - Rename externs.js to engine.externs.js. - New library_godot_runtime.js (GodotRuntime) wraps around emscripten functions. - Refactor of XMLHttpRequest handler in engine/preloader.js. - Few fixes to bugs spotted by early stage linting. --- modules/websocket/library_godot_websocket.js | 40 ++++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'modules/websocket/library_godot_websocket.js') diff --git a/modules/websocket/library_godot_websocket.js b/modules/websocket/library_godot_websocket.js index 7076a6f43d..0856cb13e6 100644 --- a/modules/websocket/library_godot_websocket.js +++ b/modules/websocket/library_godot_websocket.js @@ -28,9 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -var GodotWebSocket = { +const GodotWebSocket = { // Our socket implementation that forwards events to C++. - $GodotWebSocket__deps: ['$IDHandler'], + $GodotWebSocket__deps: ['$IDHandler', '$GodotRuntime'], $GodotWebSocket: { // Connection opened, report selected protocol _onopen: function(p_id, callback, event) { @@ -38,9 +38,9 @@ var GodotWebSocket = { if (!ref) { return; // Godot object is gone. } - let c_str = GodotOS.allocString(ref.protocol); + let c_str = GodotRuntime.allocString(ref.protocol); callback(c_str); - _free(c_str); + GodotRuntime.free(c_str); }, // Message received, report content and type (UTF8 vs binary) @@ -54,21 +54,21 @@ var GodotWebSocket = { if (event.data instanceof ArrayBuffer) { buffer = new Uint8Array(event.data); } else if (event.data instanceof Blob) { - alert("Blob type not supported"); + GodotRuntime.error("Blob type not supported"); return; } else if (typeof event.data === "string") { is_string = 1; var enc = new TextEncoder("utf-8"); buffer = new Uint8Array(enc.encode(event.data)); } else { - alert("Unknown message type"); + GodotRuntime.error("Unknown message type"); return; } var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = _malloc(len); + var out = GodotRuntime.malloc(len); HEAPU8.set(buffer, out); callback(out, len, is_string); - _free(out); + GodotRuntime.free(out); }, // An error happened, 'onclose' will be called after this. @@ -86,15 +86,15 @@ var GodotWebSocket = { if (!ref) { return; // Godot object is gone. } - let c_str = GodotOS.allocString(event.reason); + let c_str = GodotRuntime.allocString(event.reason); callback(event.code, c_str, event.wasClean ? 1 : 0); - _free(c_str); + GodotRuntime.free(c_str); }, // Send a message send: function(p_id, p_data) { const ref = IDHandler.get(p_id); - if (!ref || ref.readyState != ref.OPEN) { + if (!ref || ref.readyState !== ref.OPEN) { return 1; // Godot object is gone or socket is not in a ready state. } ref.send(p_data); @@ -115,7 +115,7 @@ var GodotWebSocket = { const ref = IDHandler.get(p_id); if (ref && ref.readyState < ref.CLOSING) { const code = p_code; - const reason = UTF8ToString(p_reason); + const reason = GodotRuntime.parseString(p_reason); ref.close(code, reason); } }, @@ -136,12 +136,12 @@ var GodotWebSocket = { }, godot_js_websocket_create: function(p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) { - const on_open = GodotOS.get_func(p_on_open).bind(null, p_ref); - const on_message = GodotOS.get_func(p_on_message).bind(null, p_ref); - const on_error = GodotOS.get_func(p_on_error).bind(null, p_ref); - const on_close = GodotOS.get_func(p_on_close).bind(null, p_ref); - const url = UTF8ToString(p_url); - const protos = UTF8ToString(p_proto); + const on_open = GodotRuntime.get_func(p_on_open).bind(null, p_ref); + const on_message = GodotRuntime.get_func(p_on_message).bind(null, p_ref); + const on_error = GodotRuntime.get_func(p_on_error).bind(null, p_ref); + const on_close = GodotRuntime.get_func(p_on_close).bind(null, p_ref); + const url = GodotRuntime.parseString(p_url); + const protos = GodotRuntime.parseString(p_proto); var socket = null; try { if (protos) { @@ -160,7 +160,7 @@ var GodotWebSocket = { var bytes_array = new Uint8Array(p_buf_len); var i = 0; for(i = 0; i < p_buf_len; i++) { - bytes_array[i] = getValue(p_buf + i, 'i8'); + bytes_array[i] = GodotRuntime.getHeapValue(p_buf + i, 'i8'); } var out = bytes_array.buffer; if (!p_raw) { @@ -171,7 +171,7 @@ var GodotWebSocket = { godot_js_websocket_close: function(p_id, p_code, p_reason) { const code = p_code; - const reason = UTF8ToString(p_reason); + const reason = GodotRuntime.parseString(p_reason); GodotWebSocket.close(p_id, code, reason); }, -- cgit v1.2.3 From 4617a7fa9cea323978f14053ef2726c84e9bd267 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 23 Nov 2020 12:13:52 +0100 Subject: [HTML5] Run eslint --fix. Should I write a poem about this whole new world? ;) --- modules/websocket/library_godot_websocket.js | 62 ++++++++++++++-------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'modules/websocket/library_godot_websocket.js') diff --git a/modules/websocket/library_godot_websocket.js b/modules/websocket/library_godot_websocket.js index 0856cb13e6..6ada4e7335 100644 --- a/modules/websocket/library_godot_websocket.js +++ b/modules/websocket/library_godot_websocket.js @@ -33,46 +33,46 @@ const GodotWebSocket = { $GodotWebSocket__deps: ['$IDHandler', '$GodotRuntime'], $GodotWebSocket: { // Connection opened, report selected protocol - _onopen: function(p_id, callback, event) { + _onopen: function (p_id, callback, event) { const ref = IDHandler.get(p_id); if (!ref) { return; // Godot object is gone. } - let c_str = GodotRuntime.allocString(ref.protocol); + const c_str = GodotRuntime.allocString(ref.protocol); callback(c_str); GodotRuntime.free(c_str); }, // Message received, report content and type (UTF8 vs binary) - _onmessage: function(p_id, callback, event) { + _onmessage: function (p_id, callback, event) { const ref = IDHandler.get(p_id); if (!ref) { return; // Godot object is gone. } - var buffer; - var is_string = 0; + let buffer; + let is_string = 0; if (event.data instanceof ArrayBuffer) { buffer = new Uint8Array(event.data); } else if (event.data instanceof Blob) { - GodotRuntime.error("Blob type not supported"); + GodotRuntime.error('Blob type not supported'); return; - } else if (typeof event.data === "string") { + } else if (typeof event.data === 'string') { is_string = 1; - var enc = new TextEncoder("utf-8"); + const enc = new TextEncoder('utf-8'); buffer = new Uint8Array(enc.encode(event.data)); } else { - GodotRuntime.error("Unknown message type"); + GodotRuntime.error('Unknown message type'); return; } - var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = GodotRuntime.malloc(len); + const len = buffer.length * buffer.BYTES_PER_ELEMENT; + const out = GodotRuntime.malloc(len); HEAPU8.set(buffer, out); callback(out, len, is_string); GodotRuntime.free(out); }, // An error happened, 'onclose' will be called after this. - _onerror: function(p_id, callback, event) { + _onerror: function (p_id, callback, event) { const ref = IDHandler.get(p_id); if (!ref) { return; // Godot object is gone. @@ -81,18 +81,18 @@ const GodotWebSocket = { }, // Connection is closed, this is always fired. Report close code, reason, and clean status. - _onclose: function(p_id, callback, event) { + _onclose: function (p_id, callback, event) { const ref = IDHandler.get(p_id); if (!ref) { return; // Godot object is gone. } - let c_str = GodotRuntime.allocString(event.reason); + const c_str = GodotRuntime.allocString(event.reason); callback(event.code, c_str, event.wasClean ? 1 : 0); GodotRuntime.free(c_str); }, // Send a message - send: function(p_id, p_data) { + send: function (p_id, p_data) { const ref = IDHandler.get(p_id); if (!ref || ref.readyState !== ref.OPEN) { return 1; // Godot object is gone or socket is not in a ready state. @@ -101,7 +101,7 @@ const GodotWebSocket = { return 0; }, - create: function(socket, p_on_open, p_on_message, p_on_error, p_on_close) { + create: function (socket, p_on_open, p_on_message, p_on_error, p_on_close) { const id = IDHandler.add(socket); socket.onopen = GodotWebSocket._onopen.bind(null, id, p_on_open); socket.onmessage = GodotWebSocket._onmessage.bind(null, id, p_on_message); @@ -111,7 +111,7 @@ const GodotWebSocket = { }, // Closes the JavaScript WebSocket (if not already closing) associated to a given C++ object. - close: function(p_id, p_code, p_reason) { + close: function (p_id, p_code, p_reason) { const ref = IDHandler.get(p_id); if (ref && ref.readyState < ref.CLOSING) { const code = p_code; @@ -121,7 +121,7 @@ const GodotWebSocket = { }, // Deletes the reference to a C++ object (closing any connected socket if necessary). - destroy: function(p_id) { + destroy: function (p_id) { const ref = IDHandler.get(p_id); if (!ref) { return; @@ -135,50 +135,50 @@ const GodotWebSocket = { }, }, - godot_js_websocket_create: function(p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) { + godot_js_websocket_create: function (p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) { const on_open = GodotRuntime.get_func(p_on_open).bind(null, p_ref); const on_message = GodotRuntime.get_func(p_on_message).bind(null, p_ref); const on_error = GodotRuntime.get_func(p_on_error).bind(null, p_ref); const on_close = GodotRuntime.get_func(p_on_close).bind(null, p_ref); const url = GodotRuntime.parseString(p_url); const protos = GodotRuntime.parseString(p_proto); - var socket = null; + let socket = null; try { if (protos) { - socket = new WebSocket(url, protos.split(",")); + socket = new WebSocket(url, protos.split(',')); } else { socket = new WebSocket(url); } } catch (e) { return 0; } - socket.binaryType = "arraybuffer"; + socket.binaryType = 'arraybuffer'; return GodotWebSocket.create(socket, on_open, on_message, on_error, on_close); }, - godot_js_websocket_send: function(p_id, p_buf, p_buf_len, p_raw) { - var bytes_array = new Uint8Array(p_buf_len); - var i = 0; - for(i = 0; i < p_buf_len; i++) { + godot_js_websocket_send: function (p_id, p_buf, p_buf_len, p_raw) { + const bytes_array = new Uint8Array(p_buf_len); + let i = 0; + for (i = 0; i < p_buf_len; i++) { bytes_array[i] = GodotRuntime.getHeapValue(p_buf + i, 'i8'); } - var out = bytes_array.buffer; + let out = bytes_array.buffer; if (!p_raw) { - out = new TextDecoder("utf-8").decode(bytes_array); + out = new TextDecoder('utf-8').decode(bytes_array); } return GodotWebSocket.send(p_id, out); }, - godot_js_websocket_close: function(p_id, p_code, p_reason) { + godot_js_websocket_close: function (p_id, p_code, p_reason) { const code = p_code; const reason = GodotRuntime.parseString(p_reason); GodotWebSocket.close(p_id, code, reason); }, - godot_js_websocket_destroy: function(p_id) { + godot_js_websocket_destroy: function (p_id) { GodotWebSocket.destroy(p_id); }, }; -autoAddDeps(GodotWebSocket, '$GodotWebSocket') +autoAddDeps(GodotWebSocket, '$GodotWebSocket'); mergeInto(LibraryManager.library, GodotWebSocket); -- cgit v1.2.3