static int UIButton_Popup(duk_context* ctx) { if (!duk_is_object(ctx, 0)) { duk_push_string(ctx, "UIButton.popup first argument must be an object"); duk_throw(ctx); } if (!duk_is_callable(ctx, 1)) { duk_push_string(ctx, "UIButton.popup second argument must be callable"); duk_throw(ctx); } JSVM* vm = JSVM::GetJSVM(ctx); duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY); UISelectItemSource* source = new UISelectItemSource(vm->GetContext()); while (duk_next(ctx, -1, 0)) { String key = duk_get_string(ctx, -1); duk_get_prop(ctx, 0); if (duk_is_array(ctx, -1)) { // need to support this, for skin image, etc assert(0); } else if (duk_is_string(ctx, -1)) { // id String id = duk_get_string(ctx, -1); source->AddItem(new UISelectItem(vm->GetContext(), key, id)); } else { duk_push_string(ctx, "UIButton.popup data object key is not an array or string"); duk_throw(ctx); } duk_pop(ctx); // pop key value } duk_pop(ctx); // pop enum object duk_push_this(ctx); duk_dup(ctx, 1); duk_put_prop_string(ctx, -2, "__popup_menu_callback"); UIButton* button = js_to_class_instance<UIButton>(ctx, -1, 0); UIMenuWindow* menuWindow = new UIMenuWindow(vm->GetContext(), button, "__popup-menu"); menuWindow->Show(source); duk_pop(ctx); return 0; }
static int js_module_read_file(duk_context* ctx) { JSVM* vm = JSVM::GetJSVM(ctx); ResourceCache* cache = vm->GetContext()->GetSubsystem<ResourceCache>(); String path = duk_to_string(ctx, 0); SharedPtr<File> file = cache->GetFile(path); if (!file->IsOpen()) { duk_push_string(ctx, "Unable to open module file"); duk_throw(ctx); return 0; } unsigned size = file->GetSize(); SharedArrayPtr<char> data; data = new char[size + 1]; data[size] = '\0'; file->Read(data, size); duk_push_string(ctx, data); return 1; }