servo: Merge #14718 - Rewrote websocket array buffer handling to typed array API (from dpyro:websocket-typed-arrays); r=jdm
authorSumant Manne <sumant.manne@gmail.com>
Wed, 28 Dec 2016 09:43:51 -0800
changeset 478606 81ac49a1c7726d97905660ab43a74114c48d9a3c
parent 478605 e084e535672be236f45471143ccfceac54d969ec
child 478607 9a191f347bef2fc2dae41243862ccc20084f21b7
push id44079
push userbmo:gps@mozilla.com
push dateSat, 04 Feb 2017 00:14:49 +0000
reviewersjdm
servo: Merge #14718 - Rewrote websocket array buffer handling to typed array API (from dpyro:websocket-typed-arrays); r=jdm <!-- Please describe your changes on the following line: -➜ Replaced existing code for handling `BinaryType::Arraybuffer` from `JS_NewArrayBuffer` to `Uint8Array::create`. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #14675 (github issue number if applicable). <!-- Either: --> - [X] These changes do not require tests because they replace an existing implementation <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> I am not certain the test suite will adequately verify my implementation as I am not familiar with the architecture. It compiles and passes the current tests. Source-Repo: https://github.com/servo/servo Source-Revision: 0f600db3ae5f1464b5d8fed169e6d50974715013
servo/components/script/dom/websocket.rs
--- a/servo/components/script/dom/websocket.rs
+++ b/servo/components/script/dom/websocket.rs
@@ -20,20 +20,19 @@ use dom::closeevent::CloseEvent;
 use dom::event::{Event, EventBubbles, EventCancelable};
 use dom::eventtarget::EventTarget;
 use dom::globalscope::GlobalScope;
 use dom::messageevent::MessageEvent;
 use dom::urlhelper::UrlHelper;
 use hyper;
 use hyper_serde::Serde;
 use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
-use js::jsapi::{JS_GetArrayBufferData, JS_NewArrayBuffer};
 use js::jsapi::JSAutoCompartment;
 use js::jsval::UndefinedValue;
-use libc::{uint32_t, uint8_t};
+use js::typedarray::ArrayBuffer;
 use net_traits::{WebSocketCommunicate, WebSocketConnectData, WebSocketDomAction, WebSocketNetworkEvent};
 use net_traits::CookieSource::HTTP;
 use net_traits::CoreResourceMsg::{SetCookiesForUrl, WebsocketConnect};
 use net_traits::MessageData;
 use net_traits::hosts::replace_hosts;
 use net_traits::unwrap_websocket_protocol;
 use script_runtime::CommonScriptMsg;
 use script_runtime::ScriptThreadEventCategory::WebSocketEvent;
@@ -603,23 +602,24 @@ impl Runnable for MessageReceivedTask {
                 MessageData::Text(text) => text.to_jsval(cx, message.handle_mut()),
                 MessageData::Binary(data) => {
                     match ws.binary_type.get() {
                         BinaryType::Blob => {
                             let blob = Blob::new(&global, BlobImpl::new_from_bytes(data), "".to_owned());
                             blob.to_jsval(cx, message.handle_mut());
                         }
                         BinaryType::Arraybuffer => {
-                            let len = data.len() as uint32_t;
-                            let buf = JS_NewArrayBuffer(cx, len);
-                            let mut is_shared = false;
-                            let buf_data: *mut uint8_t = JS_GetArrayBufferData(buf, &mut is_shared, ptr::null());
-                            assert!(!is_shared);
-                            ptr::copy_nonoverlapping(data.as_ptr(), buf_data, len as usize);
-                            buf.to_jsval(cx, message.handle_mut());
+                            rooted!(in(cx) let mut array_buffer = ptr::null_mut());
+                            assert!(ArrayBuffer::create(cx,
+                                                        data.len() as u32,
+                                                        Some(data.as_slice()),
+                                                        array_buffer.handle_mut())
+                                    .is_ok());
+
+                            (*array_buffer).to_jsval(cx, message.handle_mut());
                         }
 
                     }
                 },
             }
             MessageEvent::dispatch_jsval(ws.upcast(), &global, message.handle());
         }
     }