{{#def.interface}}
{{#def.dictionary}}
{{#def.array}}

{{

var isPrimitiveType = function (idlType) {
  var type = idlType.idlType;
  return type === 'boolean'
      || type === 'byte'
      || type === 'octet'
      || type === 'short'
      || type === 'unsigned short'
      || type === 'long'
      || type === 'unsigned long'
      || type === 'long long'
      || type === 'unsigned long long'
      || type === 'float'
      || type === 'unrestricted float'
      || type === 'double'
      || type === 'unrestricted double'
      || type === 'DOMString'
      || type === 'String';
};

var isBoolean = function (idlType) {
  return idlType.idlType === 'boolean';
};

var idlType2CxxType = function (idlType, refTypeMap) {
  var type = idlType.idlType;
  if (isArray(idlType)) {
    type = 'v8::Local<v8::Array>';
  } else if (isTypedArray(idlType)) {
    type = idlType.idlType + 'Helper';
  } else if (type === 'String' || type === 'DOMString') {
    type = 'std::string';
  } else if (type === 'boolean') {
    type = 'bool';
  } else if (type === 'byte') {
    type = 'int8_t';
  } else if (type === 'octet') {
    type = 'uint8_t';
  } else if (type === 'short') {
    type = 'int16_t';
  } else if (type === 'unsigned short') {
    type = 'uint16_t';
  } else if (type === 'long') {
    type = 'int32_t';
  } else if (type === 'unsigned long') {
    type = 'uint32_t';
  } else if (type === 'long long') {
    type = 'int64_t';
  } else if (type === 'unsigned long long') {
    type = 'uint64_t';
  } else if (type === 'float'
    || type === 'unrestricted float'
    || type === 'double'
    || type === 'unrestricted double') {
    type = 'double';
  } else if (typeof type === 'object' && idlType.generic === 'Promise') {
    type = 'v8::Handle<v8::Promise>';
  } else {
    /* If it's a referenced enum type, then use std::string */
    if (refTypeMap && refTypeMap[type]) {
      var refType = refTypeMap[type];
      if (refType.type === 'enum') {
        type = 'std::string';
      }
    }
  }
  return type;
};

var isString = function (idlType, refTypeMap) {
  return idlType2CxxType(idlType, refTypeMap) === 'std::string';
};

var isNonStringPrimitiveType = function (idlType, refTypeMap) {
  return isPrimitiveType(idlType) && !isString(idlType);
};

var isPrimitiveNumberType = function (idlType, refTypeMap) {
  return isPrimitiveType(idlType) && !isString(idlType) && !isBoolean(idlType);
};

var idlType2V8Type = function (idlType, refTypeMap, callback) {
  var type = idlType.idlType;
  if (type === 'String' || type === 'DOMString') {
    type = 'String';
  } else if (type === 'boolean') {
    type = 'Boolean';
  } else if (type === 'byte' || type === 'short' || type === 'long' || type === 'long long') {
    type = 'Number';
  } else if (type === 'octet' || type === 'unsigned short' || type === 'unsigned long' || type === 'unsigned long long') {
    type = 'Number';
  } else if (type === 'double' || type === 'float') {
    type = 'Number';
  } else if (type === 'unrestricted double' || type === 'unrestricted float') {
    type = 'Number';
  } else if (typeof type === 'object' && idlType.generic === 'Promise') {
    type = 'Promise';
  } else if (isCallback(idlType, callback)) {
    type = 'Function';
  } else if (isInterface(idlType, refTypeMap) || isDictionary(idlType, refTypeMap)) {
    type = 'Object';
  } else {
    /* If it's a referenced enum type, then use std::string */
    if (refTypeMap && refTypeMap[type]) {
      var refType = refTypeMap[type];
      if (refType.type === 'enum') {
        type = 'String';
      }
    } else {
      return 'Object';
    }

  }

  return type;
};

var idlType2V8TypeCheck = function (idlType, refTypeMap, callback) {
  return 'Is' + idlType2V8Type(idlType, refTypeMap, callback);
};

}}
