1#![deny(missing_docs)]
8
9use crate::context::JSContext;
10use crate::jsapi::{JSErrorFormatString, JSExnType, JS_ReportErrorNumberUTF8};
11use libc;
12use std::ffi::CStr;
13use std::{mem, os, ptr};
14
15static ERROR_FORMAT_STRING_STRING: &CStr = c"{0}";
17
18static mut TYPE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
20 name: c"RUSTMSG_TYPE_ERROR".as_ptr(),
21 format: ERROR_FORMAT_STRING_STRING.as_ptr(),
22 argCount: 1,
23 exnType: JSExnType::JSEXN_TYPEERR as i16,
24};
25
26static mut RANGE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
28 name: c"RUSTMSG_RANGE_ERROR".as_ptr(),
29 format: ERROR_FORMAT_STRING_STRING.as_ptr(),
30 argCount: 1,
31 exnType: JSExnType::JSEXN_RANGEERR as i16,
32};
33
34unsafe extern "C" fn get_error_message(
37 _user_ref: *mut os::raw::c_void,
38 error_number: libc::c_uint,
39) -> *const JSErrorFormatString {
40 let num: JSExnType = mem::transmute(error_number);
41 match num {
42 JSExnType::JSEXN_TYPEERR => &raw const TYPE_ERROR_FORMAT_STRING,
43 JSExnType::JSEXN_RANGEERR => &raw const RANGE_ERROR_FORMAT_STRING,
44 _ => panic!(
45 "Bad js error number given to get_error_message: {}",
46 error_number
47 ),
48 }
49}
50
51fn throw_js_error(cx: &mut JSContext, error: &CStr, error_number: u32) {
56 unsafe {
57 JS_ReportErrorNumberUTF8(
58 cx.raw_cx(),
59 Some(get_error_message),
60 ptr::null_mut(),
61 error_number,
62 error.as_ptr(),
63 )
64 };
65}
66
67pub fn throw_type_error(cx: &mut JSContext, error: &CStr) {
69 throw_js_error(cx, error, JSExnType::JSEXN_TYPEERR as u32);
70}
71
72pub fn throw_range_error(cx: &mut JSContext, error: &CStr) {
74 throw_js_error(cx, error, JSExnType::JSEXN_RANGEERR as u32);
75}
76
77pub fn throw_internal_error(cx: &mut JSContext, error: &CStr) {
79 throw_js_error(cx, error, JSExnType::JSEXN_INTERNALERR as u32);
80}