<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use core::nonzero::NonZero;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::VRPoseBinding;
use dom::bindings::codegen::Bindings::VRPoseBinding::VRPoseMethods;
use dom::bindings::js::Root;
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::globalscope::GlobalScope;
use js::jsapi::{Heap, JSContext, JSObject};
use js::typedarray::Float32Array;
use std::ptr;
use webvr_traits::webvr;

#[dom_struct]
pub struct VRPose {
    reflector_: Reflector,
    position: DOMRefCell<Heap<*mut JSObject>>,
    orientation: DOMRefCell<Heap<*mut JSObject>>,
    linear_vel: DOMRefCell<Heap<*mut JSObject>>,
    angular_vel: DOMRefCell<Heap<*mut JSObject>>,
    linear_acc: DOMRefCell<Heap<*mut JSObject>>,
    angular_acc: DOMRefCell<Heap<*mut JSObject>>
}

#[allow(unsafe_code)]
unsafe fn update_or_create_typed_array(cx: *mut JSContext,
                      src: Option<&[f32]>,
                      dst: &DOMRefCell<Heap<*mut JSObject>>) {
    let dst = dst.borrow();
    match src {
        Some(ref data) => {
            if dst.get().is_null() {
                let _ = Float32Array::create(cx, data.len() as u32, src, dst.handle_mut());
            } else {
                typedarray!(in(cx) let array: Float32Array = dst.get());
                if let Ok(mut array) = array {
                    array.update(data);
                }
            }
        },
        None => {
            if !dst.get().is_null() {
                dst.set(ptr::null_mut());
            }
        }
    }
}

#[inline]
#[allow(unsafe_code)]
fn heap_to_option(heap: &DOMRefCell<Heap<*mut JSObject>>) -> Option<NonZero<*mut JSObject>> {
    let js_object = heap.borrow_mut().get();
    if js_object.is_null() {
        None
    } else {
        unsafe {
            Some(NonZero::new(js_object))
        }
    }
}

impl VRPose {
    fn new_inherited() -> VRPose {
        VRPose {
            reflector_: Reflector::new(),
            position: DOMRefCell::new(Heap::default()),
            orientation: DOMRefCell::new(Heap::default()),
            linear_vel: DOMRefCell::new(Heap::default()),
            angular_vel: DOMRefCell::new(Heap::default()),
            linear_acc: DOMRefCell::new(Heap::default()),
            angular_acc: DOMRefCell::new(Heap::default())<commit_msg>Stop using DOMRefCell<Heap<..>>.<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use core::nonzero::NonZero;
use dom::bindings::codegen::Bindings::VRPoseBinding;
use dom::bindings::codegen::Bindings::VRPoseBinding::VRPoseMethods;
use dom::bindings::js::Root;
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::globalscope::GlobalScope;
use js::jsapi::{Heap, JSContext, JSObject};
use js::typedarray::Float32Array;
use std::ptr;
use webvr_traits::webvr;

#[dom_struct]
pub struct VRPose {
    reflector_: Reflector,
    position: Heap<*mut JSObject>,
    orientation: Heap<*mut JSObject>,
    linear_vel: Heap<*mut JSObject>,
    angular_vel: Heap<*mut JSObject>,
    linear_acc: Heap<*mut JSObject>,
    angular_acc: Heap<*mut JSObject>,
}

#[allow(unsafe_code)]
unsafe fn update_or_create_typed_array(cx: *mut JSContext,
                      src: Option<&[f32]>,
                      dst: &Heap<*mut JSObject>) {
    match src {
        Some(ref data) => {
            if dst.get().is_null() {
                let _ = Float32Array::create(cx, data.len() as u32, src, dst.handle_mut());
            } else {
                typedarray!(in(cx) let array: Float32Array = dst.get());
                if let Ok(mut array) = array {
                    array.update(data);
                }
            }
        },
        None => {
            if !dst.get().is_null() {
                dst.set(ptr::null_mut());
            }
        }
    }
}

#[inline]
#[allow(unsafe_code)]
fn heap_to_option(heap: &Heap<*mut JSObject>) -> Option<NonZero<*mut JSObject>> {
    let js_object = heap.get();
    if js_object.is_null() {
        None
    } else {
        unsafe {
            Some(NonZero::new(js_object))
        }
    }
}

impl VRPose {
    fn new_inherited() -> VRPose {
        VRPose {
            reflector_: Reflector::new(),
            position: Heap::default(),
            orientation: Heap::default(),
            linear_vel: Heap::default(),
            angular_vel: Heap::default(),
            linear_acc: Heap::default(),
            angular_acc: Heap::default(),
{'fraction_non_alphanumeric': 0.14537902388369678, 'fraction_numerical': 0.005815160955347871, 'mean_word_length': 2.6374622356495467, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3198504', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1563, 'n_words': 326}