Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/imvue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,18 @@ namespace ImVue {
void Component::configure(rapidxml::xml_node<>* node, Context* ctx, ScriptState::Context* sctx, Element* parent)
{
Context* child = newChildContext(ctx);
child->root = this;
if(child->script) {
child->script->initialize(mData);
child->script->lifecycleCallback(ScriptState::BEFORE_CREATE);
}
try {
child->root = this;
if(child->script) {
child->script->initialize(mData);
child->script->lifecycleCallback(ScriptState::BEFORE_CREATE);
}

mScriptState = ctx->script;
mScriptState = ctx->script;
} catch(...) {
delete child;
throw;
}
Element::configure(node, child, sctx, parent);
if(mConfigured) {
fireCallback(ScriptState::CREATED);
Expand Down
2 changes: 1 addition & 1 deletion src/imvue_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ namespace ImVue {

void Element::fireCallback(ScriptState::LifecycleCallbackType cb, bool schedule)
{
if(!mCtx->script) {
if(!mCtx || !mCtx->script) {
return;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/resources/bad.imv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
</template>

<script>
return {
props = {
['value-test'] = {
type = ImVue_String,
default = 'kek'
}
},
mounted = function()
self.crash()
end,
data = function()
end
}
</script>
5 changes: 4 additions & 1 deletion tests/resources/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<button v-on:click="self.count = self.count + 1">increase counter</button>
<text-unformatted>current count {{ self.count }}</text-unformatted>
</window>
<template v-if="widget == 'basic'">
<template v-if="widget == 'bad'">
<bad/>
</template>
<template v-else-if="widget == 'basic'">
<widget :count="self.count"/>
</template>
<template v-else-if="widget == 'lifecycle'">
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,29 @@ TEST_F(LuaScriptStateTest, TestComponentReactivity)
EXPECT_STREQ(local[0]->text, "updated");
}

TEST_F(LuaScriptStateTest, TestComponentCreationError)
{
ImVue::LuaScriptState* state = new ImVue::LuaScriptState(L);
ImVue::Context* ctx = ImVue::createContext(
ImVue::createElementFactory(),
state
);
luaL_dostring(L, "widget = 'bad'");
ImVue::Document document(ctx);
char* data = ctx->fs->load("components.xml");
ASSERT_NE((size_t)data, 0);
EXPECT_THROW({
try{
document.parse(data);
renderDocument(document);
} catch(ImVue::ElementError e) {
std::cout << e.what() << "\n";
throw;
}
}, ImVue::ScriptError);
ImGui::MemFree(data);
}

TEST_F(LuaScriptStateTest, TestComponentLifecycle)
{
ImVue::LuaScriptState* state = new ImVue::LuaScriptState(L);
Expand Down