Skip to content

Commit

Permalink
Merge pull request #2042 from xodio/fix-tweak-strings
Browse files Browse the repository at this point in the history
Copy values bound to `tweak-string`s directly into buffers
  • Loading branch information
evgenykochetkov authored Sep 18, 2020
2 parents ce8f9cb + f01bb0c commit 333006b
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 70 deletions.
29 changes: 22 additions & 7 deletions packages/xod-arduino/platform/program.tpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ typedef {{ ns patch }}::Node{{#if (or (containsConstantInputs inputs) (containsT
>
{{~/if}} Node_{{ id }};
Node_{{ id }} node_{{ id }} = Node_{{ id }}(
{{~#if (isTweakStringNode this) ~}}
XString()
{{~else~}}
{{~#eachNonPulseOrConstant outputs~}}
{{ cppValue type value }}
{{~#unless @last}}, {{/unless}}
{{~/eachNonPulseOrConstant~}}
{{~/if~}}
);
{{/unless}}
{{/each}}
Expand Down Expand Up @@ -156,6 +160,15 @@ void handleDebugProtocolMessages() {
} // namespace detail
#endif

// Copy values bound to `tweak-string`s outputs
// directly into buffers instead of wasting memory
// on XStringCString with initial values
void initializeTweakStrings() {
{{#eachTweakStringNode nodes}}
strncpy(node_{{ id }}.state.buff, "{{#each outputs}}{{ escapeCppString value }}{{/each}}", {{ getStringTweakLength patch.patchPath }});
{{/eachTweakStringNode}}
}

void handleDefers() {
{{#eachDeferNode nodes}}
{
Expand Down Expand Up @@ -293,13 +306,15 @@ void runTransaction() {
g_transaction.node_{{id}}_isNodeDirty |= detail::isTimedOut(&node_{{id}});
{{/eachNodeUsingTimeouts}}

// defer-* nodes are always at the very bottom of the graph, so no one will
// recieve values emitted by them. We must evaluate them before everybody
// else to give them a chance to emit values.
//
// If trigerred, keep only output dirty, not the node itself, so it will
// evaluate on the regular pass only if it receives a new value again.
if (!isSettingUp()) {
if (isSettingUp()) {
initializeTweakStrings();
} else {
// defer-* nodes are always at the very bottom of the graph, so no one will
// recieve values emitted by them. We must evaluate them before everybody
// else to give them a chance to emit values.
//
// If trigerred, keep only output dirty, not the node itself, so it will
// evaluate on the regular pass only if it receives a new value again.
g_isEarlyDeferPass = true;
handleDefers();
g_isEarlyDeferPass = false;
Expand Down
15 changes: 14 additions & 1 deletion packages/xod-arduino/src/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const cppType = def(
)
);

const escapeCppString = str => R.replace(/"/g, '\\"', unquote(str));

// Formats a plain JS string into C++ string object
const cppStringLiteral = def(
'cppStringLiteral :: String -> String',
Expand All @@ -111,7 +113,7 @@ const cppStringLiteral = def(
R.tail, // `XOD_LITERAL`
],
// All other strings: "Hello, world"
[R.T, str => `XStringCString("${R.replace(/"/g, '\\"', unquote(str))}")`],
[R.T, str => `XStringCString("${escapeCppString(str)}")`],
])
);

Expand Down Expand Up @@ -258,6 +260,8 @@ Handlebars.registerHelper('cppValue', (type, value) =>
})(value)
);

Handlebars.registerHelper('escapeCppString', escapeCppString);

const hasUpstreamErrorRaisers = nodeOrInput =>
R.pathOr(0, ['upstreamErrorRaisers', 'length'], nodeOrInput) > 0;

Expand Down Expand Up @@ -285,6 +289,14 @@ const isLinkedTweakNode = R.both(

Handlebars.registerHelper('isLinkedTweakNode', isLinkedTweakNode);

const isTweakStringNode = R.pipe(
R.path(['patch', 'patchPath']),
XP.getTweakType,
R.equals(XP.PIN_TYPE.STRING)
);

Handlebars.registerHelper('isTweakStringNode', isTweakStringNode);

Handlebars.registerHelper('isPulse', R.equals(XP.PIN_TYPE.PULSE));

const isTemplatableCustomTypePin = R.prop('isTemplatableCustomTypePin');
Expand Down Expand Up @@ -371,6 +383,7 @@ registerHandlebarsFilterLoopHelper(
R.path(['patch', 'usesTimeouts'])
);
registerHandlebarsFilterLoopHelper('eachLinkedTweakNode', isLinkedTweakNode);
registerHandlebarsFilterLoopHelper('eachTweakStringNode', isTweakStringNode);
registerHandlebarsFilterLoopHelper('eachLinkedInput', R.has('fromNodeId'));
registerHandlebarsFilterLoopHelper(
'eachNonlinkedInput',
Expand Down
6 changes: 1 addition & 5 deletions workspace/__lib__/xod/debug/tweak-string-128/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,5 @@ void evaluate(Context ctx) {
// Additional code that sets value of `buff`
// is injected in detail::handleTweaks

// don't emit on startup to prevent overwriting value
// that was initially bound (which is stored in the global autogenerated CString)
if (!isSettingUp()) {
emitValue<output_OUT>(ctx, XString(&state->view));
}
emitValue<output_OUT>(ctx, XString(&state->view));
}
6 changes: 1 addition & 5 deletions workspace/__lib__/xod/debug/tweak-string-16/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,5 @@ void evaluate(Context ctx) {
// Additional code that sets value of `buff`
// is injected in detail::handleTweaks

// don't emit on startup to prevent overwriting value
// that was initially bound (which is stored in the global autogenerated CString)
if (!isSettingUp()) {
emitValue<output_OUT>(ctx, XString(&state->view));
}
emitValue<output_OUT>(ctx, XString(&state->view));
}
6 changes: 1 addition & 5 deletions workspace/__lib__/xod/debug/tweak-string-32/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,5 @@ void evaluate(Context ctx) {
// Additional code that sets value of `buff`
// is injected in detail::handleTweaks

// don't emit on startup to prevent overwriting value
// that was initially bound (which is stored in the global autogenerated CString)
if (!isSettingUp()) {
emitValue<output_OUT>(ctx, XString(&state->view));
}
emitValue<output_OUT>(ctx, XString(&state->view));
}
6 changes: 1 addition & 5 deletions workspace/__lib__/xod/debug/tweak-string-64/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,5 @@ void evaluate(Context ctx) {
// Additional code that sets value of `buff`
// is injected in detail::handleTweaks

// don't emit on startup to prevent overwriting value
// that was initially bound (which is stored in the global autogenerated CString)
if (!isSettingUp()) {
emitValue<output_OUT>(ctx, XString(&state->view));
}
emitValue<output_OUT>(ctx, XString(&state->view));
}
22 changes: 15 additions & 7 deletions workspace/big-patch/__fixtures__/arduino.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions workspace/blink/__fixtures__/arduino.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions workspace/count-with-feedback-loops/__fixtures__/arduino.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions workspace/dht-pack-unpack/__fixtures__/arduino.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions workspace/lcd-time/__fixtures__/arduino.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions workspace/two-button-switch/__fixtures__/arduino.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 333006b

Please sign in to comment.