scope nested component props without deep copies
This commit is contained in:
+24
-10
@@ -494,20 +494,32 @@ struct RequestPropsScope
|
||||
Request* context = 0;
|
||||
DValue previous_props;
|
||||
|
||||
RequestPropsScope(Request* context, const DValue& props)
|
||||
static void swap_value(DValue& left, DValue& right)
|
||||
{
|
||||
std::swap(left.type, right.type);
|
||||
left._String.swap(right._String);
|
||||
std::swap(left._float, right._float);
|
||||
std::swap(left._array_index, right._array_index);
|
||||
std::swap(left._bool, right._bool);
|
||||
std::swap(left._list_mode, right._list_mode);
|
||||
std::swap(left._ptr, right._ptr);
|
||||
left._map.swap(right._map);
|
||||
}
|
||||
|
||||
RequestPropsScope(Request* context, DValue& props)
|
||||
{
|
||||
this->context = context;
|
||||
if(this->context)
|
||||
{
|
||||
previous_props = this->context->props;
|
||||
this->context->props = props;
|
||||
swap_value(previous_props, this->context->props);
|
||||
swap_value(this->context->props, props);
|
||||
}
|
||||
}
|
||||
|
||||
~RequestPropsScope()
|
||||
{
|
||||
if(context)
|
||||
context->props = previous_props;
|
||||
swap_value(context->props, previous_props);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -600,7 +612,8 @@ DValue* unit_call(String file_name, String function_name, DValue* call_param)
|
||||
print("Error: unit_call() function '", function_name, "' not found");
|
||||
return(0);
|
||||
}
|
||||
RequestPropsScope props_scope(context, (call_param ? *call_param : DValue()));
|
||||
DValue props = call_param ? *call_param : DValue();
|
||||
RequestPropsScope props_scope(context, props);
|
||||
if((handler == "render" || handler.rfind("render:", 0) == 0 || handler == "component" || handler.rfind("component:", 0) == 0) && resolved != "")
|
||||
wasm_run_once(resolved, *context);
|
||||
String previous_unit = context->resources.current_unit_file;
|
||||
@@ -634,7 +647,7 @@ DValue* unit_call(String file_name, String function_name, DValue* call_param)
|
||||
return(0);
|
||||
}
|
||||
|
||||
void component_render(String name, DValue props, Request& request)
|
||||
static void component_render_with_props(String name, DValue& props, Request& request)
|
||||
{
|
||||
String file_name, render_name;
|
||||
component_parse_target(trim(name), file_name, render_name);
|
||||
@@ -658,14 +671,15 @@ void component_render(String name, DValue props, Request& request)
|
||||
request.resources.current_unit_file = previous_unit;
|
||||
}
|
||||
|
||||
void component_render(String name) { DValue props; component_render(name, props, *context); }
|
||||
void component_render(String name, Request& request) { DValue props; component_render(name, props, request); }
|
||||
void component_render(String name, DValue props) { component_render(name, props, *context); }
|
||||
void component_render(String name, DValue props, Request& request) { component_render_with_props(name, props, request); }
|
||||
void component_render(String name) { DValue props; component_render_with_props(name, props, *context); }
|
||||
void component_render(String name, Request& request) { DValue props; component_render_with_props(name, props, request); }
|
||||
void component_render(String name, DValue props) { component_render_with_props(name, props, *context); }
|
||||
|
||||
String component(String name, DValue props, Request& request)
|
||||
{
|
||||
ob_start();
|
||||
component_render(name, props, request);
|
||||
component_render_with_props(name, props, request);
|
||||
return(ob_get_close());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user