javascripthelpers.swg 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. %insert(wrapper) %{
  2. SWIGINTERN bool JS_registerClass(JSGlobalContextRef context, JSObjectRef parentObject,
  3. const char* className,
  4. JSClassDefinition* definition) {
  5. JSStringRef js_className = JSStringCreateWithUTF8CString(className);
  6. JSObjectRef classObject = JSObjectMake(context, JSClassCreate(definition), NULL);
  7. JSObjectSetProperty(context, parentObject,
  8. js_className, classObject,
  9. kJSPropertyAttributeNone, NULL);
  10. JSStringRelease(js_className);
  11. return true;
  12. }
  13. SWIGINTERN bool JS_registerNamespace(JSGlobalContextRef context,
  14. JSObjectRef namespaceObj, JSObjectRef parentNamespace,
  15. const char* name)
  16. {
  17. JSStringRef js_name = JSStringCreateWithUTF8CString(name);
  18. JSObjectSetProperty(context, parentNamespace,
  19. js_name, namespaceObj,
  20. kJSPropertyAttributeNone, NULL);
  21. JSStringRelease(js_name);
  22. return true;
  23. }
  24. SWIGINTERN bool JS_registerFunction(JSGlobalContextRef context, JSObjectRef object,
  25. const char* functionName, JSObjectCallAsFunctionCallback callback)
  26. {
  27. JSStringRef js_functionName = JSStringCreateWithUTF8CString(functionName);
  28. JSObjectSetProperty(context, object, js_functionName,
  29. JSObjectMakeFunctionWithCallback(context, js_functionName, callback),
  30. kJSPropertyAttributeNone, NULL);
  31. JSStringRelease(js_functionName);
  32. return true;
  33. }
  34. SWIGINTERN bool JS_veto_set_variable(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
  35. {
  36. char buffer[256];
  37. char msg[512];
  38. int res;
  39. JSStringGetUTF8CString(propertyName, buffer, 256);
  40. res = sprintf(msg, "Tried to write read-only variable: %s.", buffer);
  41. if(res<0) {
  42. SWIG_exception(SWIG_ERROR, "Tried to write read-only variable.");
  43. } else {
  44. SWIG_exception(SWIG_ERROR, msg);
  45. }
  46. return false;
  47. }
  48. SWIGINTERN JSValueRef JS_CharPtrToJSValue(JSContextRef context, char* cstr) {
  49. JSValueRef val;
  50. JSStringRef jsstring = JSStringCreateWithUTF8CString((char*) cstr);
  51. val = JSValueMakeString(context, jsstring);
  52. JSStringRelease(jsstring);
  53. return val;
  54. }
  55. %}