codegen: Throw a more descriptive invalid enum message

This commit is contained in:
Emilio Cobos Álvarez 2016-05-13 10:36:41 +02:00
parent e50d4b7624
commit 91101f6226
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
2 changed files with 10 additions and 9 deletions

View file

@ -662,11 +662,11 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
'%s' % (firstCap(sourceDescription), exceptionCode))), '%s' % (firstCap(sourceDescription), exceptionCode))),
post="\n") post="\n")
def onFailureInvalidEnumValue(failureCode): def onFailureInvalidEnumValue(failureCode, passedVarName):
return CGGeneric( return CGGeneric(
failureCode or failureCode or
('throw_type_error(cx, "%s is not a valid enum value."); %s' ('throw_type_error(cx, &format!("\'{}\' is not a valid enum value for enumeration \'%s\'.", %s)); %s'
% (firstCap(sourceDescription), exceptionCode))) % (type.name, passedVarName, exceptionCode)))
def onFailureNotCallable(failureCode): def onFailureNotCallable(failureCode):
return CGGeneric( return CGGeneric(
@ -873,15 +873,15 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
"yet") "yet")
enum = type.inner.identifier.name enum = type.inner.identifier.name
if invalidEnumValueFatal: if invalidEnumValueFatal:
handleInvalidEnumValueCode = onFailureInvalidEnumValue(failureCode).define() handleInvalidEnumValueCode = onFailureInvalidEnumValue(failureCode, 'search').define()
else: else:
handleInvalidEnumValueCode = "return true;" handleInvalidEnumValueCode = "return true;"
template = ( template = (
"match find_enum_string_index(cx, ${val}, %(values)s) {\n" "match find_enum_string_index(cx, ${val}, %(values)s) {\n"
" Err(_) => { %(exceptionCode)s },\n" " Err(_) => { %(exceptionCode)s },\n"
" Ok(None) => { %(handleInvalidEnumValueCode)s },\n" " Ok((None, search)) => { %(handleInvalidEnumValueCode)s },\n"
" Ok(Some(index)) => {\n" " Ok((Some(index), _)) => {\n"
" //XXXjdm need some range checks up in here.\n" " //XXXjdm need some range checks up in here.\n"
" mem::transmute(index)\n" " mem::transmute(index)\n"
" },\n" " },\n"

View file

@ -39,6 +39,7 @@ use std::ptr;
use std::slice; use std::slice;
use util::non_geckolib::jsstring_to_str; use util::non_geckolib::jsstring_to_str;
use util::prefs; use util::prefs;
use util::str::DOMString;
/// Proxy handler for a WindowProxy. /// Proxy handler for a WindowProxy.
pub struct WindowProxyHandler(pub *const libc::c_void); pub struct WindowProxyHandler(pub *const libc::c_void);
@ -182,18 +183,18 @@ pub fn get_array_index_from_id(_cx: *mut JSContext, id: HandleId) -> Option<u32>
/// Find the index of a string given by `v` in `values`. /// Find the index of a string given by `v` in `values`.
/// Returns `Err(())` on JSAPI failure (there is a pending exception), and /// Returns `Err(())` on JSAPI failure (there is a pending exception), and
/// `Ok(None)` if there was no matching string. /// `Ok((None, value))` if there was no matching string.
pub unsafe fn find_enum_string_index(cx: *mut JSContext, pub unsafe fn find_enum_string_index(cx: *mut JSContext,
v: HandleValue, v: HandleValue,
values: &[&'static str]) values: &[&'static str])
-> Result<Option<usize>, ()> { -> Result<(Option<usize>, DOMString), ()> {
let jsstr = ToString(cx, v); let jsstr = ToString(cx, v);
if jsstr.is_null() { if jsstr.is_null() {
return Err(()); return Err(());
} }
let search = jsstring_to_str(cx, jsstr); let search = jsstring_to_str(cx, jsstr);
Ok(values.iter().position(|value| search == *value)) Ok((values.iter().position(|value| search == *value), search))
} }
/// Returns wether `obj` is a platform object /// Returns wether `obj` is a platform object