JSIO.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "JSIO.h"
  5. #include "JSVM.h"
  6. #include <Atomic/IO/File.h>
  7. namespace Atomic
  8. {
  9. static int File_ReadText(duk_context* ctx)
  10. {
  11. duk_push_this(ctx);
  12. File* file = js_to_class_instance<File>(ctx, -1, 0);
  13. String text;
  14. file->ReadText(text);
  15. duk_push_string(ctx, text.CString());
  16. return 1;
  17. }
  18. static int FileSystem_ScanDir(duk_context* ctx)
  19. {
  20. duk_push_this(ctx);
  21. FileSystem* fs = js_to_class_instance<FileSystem>(ctx, -1, 0);
  22. if ( !duk_is_string(ctx, 0) || !duk_is_string(ctx, 1) ||
  23. !duk_is_number(ctx, 2) || !duk_is_boolean(ctx, 3))
  24. {
  25. duk_push_string(ctx, "FileSystem::ScanDir bad args");
  26. duk_throw(ctx);
  27. }
  28. const char* pathName = duk_to_string(ctx, 0);
  29. const char* filter = duk_to_string(ctx, 1);
  30. unsigned flags = duk_to_number(ctx, 2);
  31. bool recursive = duk_to_boolean(ctx, 3) ? true : false;
  32. Vector<String> result;
  33. fs->ScanDir(result, pathName, filter, flags, recursive);
  34. duk_push_array(ctx);
  35. for (unsigned i = 0; i < result.Size(); i++)
  36. {
  37. duk_push_string(ctx, result[i].CString());
  38. duk_put_prop_index(ctx, -2, i);
  39. }
  40. return 1;
  41. }
  42. void jsapi_init_io(JSVM* vm)
  43. {
  44. duk_context* ctx = vm->GetJSContext();
  45. js_class_get_prototype(ctx, "File");
  46. duk_push_c_function(ctx, File_ReadText, 0);
  47. duk_put_prop_string(ctx, -2, "readText");
  48. duk_pop(ctx);
  49. js_class_get_prototype(ctx, "FileSystem");
  50. duk_push_c_function(ctx, FileSystem_ScanDir, 4);
  51. duk_put_prop_string(ctx, -2, "scanDir");
  52. duk_pop(ctx);
  53. }
  54. }