javascriptstrings.swg 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* ------------------------------------------------------------
  2. * utility methods for char strings
  3. * ------------------------------------------------------------ */
  4. %fragment("SWIG_AsCharPtrAndSize", "header", fragment="SWIG_pchar_descriptor") {
  5. SWIGINTERN int
  6. SWIG_AsCharPtrAndSize(v8::Handle<v8::Value> valRef, char** cptr, size_t* psize, int *alloc)
  7. {
  8. if(valRef->IsString()) {
  9. v8::Handle<v8::String> js_str = valRef->ToString();
  10. size_t len = js_str->Utf8Length() + 1;
  11. char* cstr = new char[len];
  12. js_str->WriteUtf8(cstr, len);
  13. if(alloc) *alloc = SWIG_NEWOBJ;
  14. if(psize) *psize = len;
  15. if(cptr) *cptr = cstr;
  16. return SWIG_OK;
  17. } else {
  18. if(valRef->IsObject()) {
  19. v8::Handle<v8::Object> obj = valRef->ToObject();
  20. // try if the object is a wrapped char[]
  21. swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
  22. if (pchar_descriptor) {
  23. void* vptr = 0;
  24. if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) {
  25. if (cptr) *cptr = (char *) vptr;
  26. if (psize) *psize = vptr ? (strlen((char *)vptr) + 1) : 0;
  27. if (alloc) *alloc = SWIG_OLDOBJ;
  28. return SWIG_OK;
  29. }
  30. }
  31. return SWIG_TypeError;
  32. } else {
  33. return SWIG_TypeError;
  34. }
  35. }
  36. }
  37. }
  38. %fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") {
  39. SWIGINTERNINLINE v8::Handle<v8::Value>
  40. SWIG_FromCharPtrAndSize(const char* carray, size_t size)
  41. {
  42. if (carray) {
  43. if (size > INT_MAX) {
  44. // TODO: handle extra long strings
  45. return SWIGV8_UNDEFINED();
  46. } else {
  47. v8::Handle<v8::String> js_str = SWIGV8_STRING_NEW2(carray, size);
  48. return js_str;
  49. }
  50. } else {
  51. return SWIGV8_UNDEFINED();
  52. }
  53. }
  54. }