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