aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/script_runtime.rs
diff options
context:
space:
mode:
authorRidhim Rastogi <ridhimrastogi@gmail.com>2019-10-26 18:44:12 -0400
committerRidhim Rastogi <ridhimrastogi@gmail.com>2019-11-03 03:41:41 -0500
commit9fb01e879ba5fa110a171b36189e043a201a14eb (patch)
tree2a855ebc6053ce115277b280cba3fbbbc4a22a5f /components/script/script_runtime.rs
parent4ad08fff04d974855845bb1bafe10920bfd27a97 (diff)
downloadservo-9fb01e879ba5fa110a171b36189e043a201a14eb.tar.gz
servo-9fb01e879ba5fa110a171b36189e043a201a14eb.zip
Add StreamConsumer wrapper and ReportStreamErrorCallback fn
Diffstat (limited to 'components/script/script_runtime.rs')
-rw-r--r--components/script/script_runtime.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs
index 21cea763c00..10198050ace 100644
--- a/components/script/script_runtime.rs
+++ b/components/script/script_runtime.rs
@@ -5,6 +5,8 @@
//! The script runtime contains common traits and structs commonly used by the
//! script thread, the dom, and the worker threads.
+#![allow(dead_code)]
+
use crate::dom::bindings::codegen::Bindings::PromiseBinding::PromiseJobCallback;
use crate::dom::bindings::conversions::get_dom_class;
use crate::dom::bindings::conversions::private_from_object;
@@ -26,7 +28,10 @@ use crate::script_thread::trace_thread;
use crate::task::TaskBox;
use crate::task_source::{TaskSource, TaskSourceName};
use js::glue::{CollectServoSizes, CreateJobQueue, DeleteJobQueue, JobQueueTraps, SetBuildId};
+use js::glue::{RUST_js_GetErrorMessage, StreamConsumerConsumeChunk, StreamConsumerStreamEnd};
+use js::glue::{StreamConsumerNoteResponseURLs, StreamConsumerStreamError};
use js::jsapi::ContextOptionsRef;
+use js::jsapi::StreamConsumer as JSStreamConsumer;
use js::jsapi::{BuildIdCharVector, DisableIncrementalGC, GCDescription, GCProgress};
use js::jsapi::{HandleObject, Heap, JobQueue};
use js::jsapi::{JSContext as RawJSContext, JSTracer, SetDOMCallbacks, SetGCSliceCallback};
@@ -51,6 +56,7 @@ use profile_traits::mem::{Report, ReportKind, ReportsChan};
use servo_config::opts;
use servo_config::pref;
use std::cell::Cell;
+use std::ffi::CString;
use std::fmt;
use std::io::{stdout, Write};
use std::ops::Deref;
@@ -776,3 +782,53 @@ impl Deref for JSContext {
&self.0
}
}
+
+pub struct StreamConsumer(*mut JSStreamConsumer);
+
+#[allow(unsafe_code)]
+impl StreamConsumer {
+ fn consume_chunk(&self, stream: &[u8]) -> bool {
+ unsafe {
+ let stream_ptr = stream.as_ptr();
+ return StreamConsumerConsumeChunk(self.0, stream_ptr, stream.len());
+ }
+ }
+
+ fn stream_end(&self) {
+ unsafe {
+ StreamConsumerStreamEnd(self.0);
+ }
+ }
+
+ fn stream_error(&self, error_code: usize) {
+ unsafe {
+ StreamConsumerStreamError(self.0, error_code);
+ }
+ }
+
+ fn note_response_urls(&self, maybe_url: Option<String>, maybe_source_map_url: Option<String>) {
+ unsafe {
+ let maybe_url = maybe_url.map(|url| CString::new(url).unwrap());
+ let maybe_source_map_url = maybe_source_map_url.map(|url| CString::new(url).unwrap());
+
+ let maybe_url_param = match maybe_url.as_ref() {
+ Some(url) => url.as_ptr(),
+ None => ptr::null(),
+ };
+ let maybe_source_map_url_param = match maybe_source_map_url.as_ref() {
+ Some(url) => url.as_ptr(),
+ None => ptr::null(),
+ };
+
+ StreamConsumerNoteResponseURLs(self.0, maybe_url_param, maybe_source_map_url_param);
+ }
+ }
+}
+
+#[allow(unsafe_code)]
+unsafe extern "C" fn report_stream_error_callback(_cx: *mut JSContext, error_code: usize) {
+ error!(
+ "Error initializing StreamConsumer: {:?}",
+ RUST_js_GetErrorMessage(ptr::null_mut(), error_code as u32)
+ );
+}