Skip to content

Commit

Permalink
Merge pull request #4 from Tasssadar/gridui_mem
Browse files Browse the repository at this point in the history
feat: reduce gridui memory usage by using `exotic` class methods
  • Loading branch information
cubicap committed Jul 8, 2024
2 parents b9912c7 + 78f9379 commit 45f6651
Show file tree
Hide file tree
Showing 38 changed files with 1,212 additions and 638 deletions.
3 changes: 2 additions & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
set(JAC_ESP32_VERSION "0.0.9")

idf_component_register(
SRCS "main.cpp" "platform/espWifi.cpp" "platform/espNvsKeyValue.cpp" "espFeatures/gridui/gridUiFeature.cpp"
SRCS "main.cpp" "platform/espWifi.cpp" "platform/espNvsKeyValue.cpp"
"espFeatures/gridui/gridUiFeature.cpp" "espFeatures/gridui/widgets/_common.cpp"
INCLUDE_DIRS ""
REQUIRES jac-dcore jac-machine jac-link
driver pthread spiffs vfs fatfs
Expand Down
19 changes: 10 additions & 9 deletions main/espFeatures/gridui/builder/arm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

#include <jac/machine/functionFactory.h>
#include <gridui.h>
#include "../widgets/arm.h"

#include "../widgets/_common.h"

namespace gridui_jac {

class ArmBuilder {
static JSValue info(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Arm*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Arm>(thisVal);
auto json = jac::ValueWeak(ctx_, argv[0]).to<std::string>();
builder.info(std::unique_ptr<rbjson::Object>(rbjson::parse((char*)json.c_str(), json.size())));
return JS_DupValue(ctx_, thisVal);
}


public:
static jac::Object proto(jac::ContextRef ctx) {
static JSCFunction *getPropFunc(const AtomString& name) {
using namespace gridui;

auto proto = jac::Object::create(ctx);
if(name == "css") return builderCss<builder::Arm>;
if(name == "finish") return builderFinish<WidgetTypeId::Arm, builder::Arm, Arm>;

proto.set("info", jac::Value(ctx, JS_NewCFunction(ctx, info, "info", 1)));
if(name == "info") return info;

defineBuilderCallback<builder::Arm, Arm, &builder::Arm::onGrab>(ctx, proto, "onGrab");
defineBuilderCallback<builder::Arm, Arm, &builder::Arm::onPositionChanged>(ctx, proto, "onPositionChanged");
if(name == "onGrab") return &builderCallbackImpl<builder::Arm, Arm, &builder::Arm::onGrab>;
if(name == "onPositionChanged") return &builderCallbackImpl<builder::Arm, Arm, &builder::Arm::onPositionChanged>;

return proto;
return nullptr;
}
};

Expand Down
33 changes: 18 additions & 15 deletions main/espFeatures/gridui/builder/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,62 @@
#include <jac/machine/functionFactory.h>
#include <gridui.h>

#include "../widgets/_common.h"

namespace gridui_jac {

class BarBuilder {
static JSValue color(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Bar*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Bar>(thisVal);
builder.color(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue fontSize(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Bar*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Bar>(thisVal);
builder.fontSize(jac::ValueWeak(ctx_, argv[0]).to<float>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue min(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Bar*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Bar>(thisVal);
builder.min(jac::ValueWeak(ctx_, argv[0]).to<float>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue max(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Bar*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Bar>(thisVal);
builder.max(jac::ValueWeak(ctx_, argv[0]).to<float>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue value(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Bar*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Bar>(thisVal);
builder.value(jac::ValueWeak(ctx_, argv[0]).to<float>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue showValue(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Bar*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Bar>(thisVal);
builder.showValue(jac::ValueWeak(ctx_, argv[0]).to<bool>());
return JS_DupValue(ctx_, thisVal);
}

public:
static jac::Object proto(jac::ContextRef ctx) {
static JSCFunction *getPropFunc(const AtomString& name) {
using namespace gridui;

auto proto = jac::Object::create(ctx);
if(name == "css") return builderCss<builder::Bar>;
if(name == "finish") return builderFinish<WidgetTypeId::Bar, builder::Bar, Bar>;

proto.set("color", jac::Value(ctx, JS_NewCFunction(ctx, color, "color", 1)));
proto.set("fontSize", jac::Value(ctx, JS_NewCFunction(ctx, fontSize, "fontSize", 1)));
proto.set("min", jac::Value(ctx, JS_NewCFunction(ctx, min, "min", 1)));
proto.set("max", jac::Value(ctx, JS_NewCFunction(ctx, max, "max", 1)));
proto.set("value", jac::Value(ctx, JS_NewCFunction(ctx, value, "value", 1)));
proto.set("showValue", jac::Value(ctx, JS_NewCFunction(ctx, showValue, "showValue", 1)));
if(name == "color") return color;
if(name == "fontSize") return fontSize;
if(name == "min") return min;
if(name == "max") return max;
if(name == "value") return value;
if(name == "showValue") return showValue;

return proto;
return nullptr;
}
};

Expand Down
41 changes: 22 additions & 19 deletions main/espFeatures/gridui/builder/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,72 @@
#include <jac/machine/functionFactory.h>
#include <gridui.h>

#include "../widgets/_common.h"

namespace gridui_jac {

class ButtonBuilder {
static JSValue text(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Button*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Button>(thisVal);
builder.text(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue fontSize(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Button*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Button>(thisVal);
builder.fontSize(jac::ValueWeak(ctx_, argv[0]).to<float>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue color(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Button*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Button>(thisVal);
builder.color(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue background(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Button*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Button>(thisVal);
builder.background(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue align(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Button*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Button>(thisVal);
builder.align(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue valign(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Button*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Button>(thisVal);
builder.valign(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue disabled(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Button*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Button>(thisVal);
builder.disabled(jac::ValueWeak(ctx_, argv[0]).to<bool>());
return JS_DupValue(ctx_, thisVal);
}

public:
static jac::Object proto(jac::ContextRef ctx) {
static JSCFunction *getPropFunc(const AtomString& name) {
using namespace gridui;

auto proto = jac::Object::create(ctx);
if(name == "css") return builderCss<builder::Button>;
if(name == "finish") return builderFinish<WidgetTypeId::Button, builder::Button, Button>;

proto.set("text", jac::Value(ctx, JS_NewCFunction(ctx, text, "text", 1)));
proto.set("fontSize", jac::Value(ctx, JS_NewCFunction(ctx, fontSize, "fontSize", 1)));
proto.set("color", jac::Value(ctx, JS_NewCFunction(ctx, color, "color", 1)));
proto.set("background", jac::Value(ctx, JS_NewCFunction(ctx, background, "background", 1)));
proto.set("align", jac::Value(ctx, JS_NewCFunction(ctx, align, "align", 1)));
proto.set("valign", jac::Value(ctx, JS_NewCFunction(ctx, valign, "valign", 1)));
proto.set("disabled", jac::Value(ctx, JS_NewCFunction(ctx, disabled, "disabled", 1)));
if(name == "text") return text;
if(name == "fontSize") return fontSize;
if(name == "color") return color;
if(name == "background") return background;
if(name == "align") return align;
if(name == "valign") return valign;
if(name == "disabled") return disabled;

defineBuilderCallback<builder::Button, Button, &builder::Button::onPress>(ctx, proto, "onPress");
defineBuilderCallback<builder::Button, Button, &builder::Button::onRelease>(ctx, proto, "onRelease");
if(name == "onPress") return &builderCallbackImpl<builder::Button, Button, &builder::Button::onPress>;
if(name == "onRelease") return &builderCallbackImpl<builder::Button, Button, &builder::Button::onRelease>;

return proto;
return nullptr;
}
};

Expand Down
18 changes: 11 additions & 7 deletions main/espFeatures/gridui/builder/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@
#include <jac/machine/functionFactory.h>
#include <gridui.h>

#include "../widgets/_common.h"

namespace gridui_jac {

class CameraBuilder {
static JSValue rotation(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Camera*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Camera>(thisVal);
builder.rotation(jac::ValueWeak(ctx_, argv[0]).to<float>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue clip(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Camera*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Camera>(thisVal);
builder.clip(jac::ValueWeak(ctx_, argv[0]).to<bool>());
return JS_DupValue(ctx_, thisVal);
}


public:
static jac::Object proto(jac::ContextRef ctx) {
static JSCFunction *getPropFunc(const AtomString& name) {
using namespace gridui;

auto proto = jac::Object::create(ctx);
if(name == "css") return builderCss<builder::Camera>;
if(name == "finish") return builderFinish<WidgetTypeId::Camera, builder::Camera, Camera>;

proto.set("rotation", jac::Value(ctx, JS_NewCFunction(ctx, rotation, "rotation", 1)));
proto.set("clip", jac::Value(ctx, JS_NewCFunction(ctx, clip, "clip", 1)));
if(name == "rotation") return rotation;
if(name == "clip") return clip;

return proto;
return nullptr;
}
};

Expand Down
27 changes: 15 additions & 12 deletions main/espFeatures/gridui/builder/checkbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,50 @@
#include <jac/machine/functionFactory.h>
#include <gridui.h>

#include "../widgets/_common.h"

namespace gridui_jac {

class CheckboxBuilder {
static JSValue fontSize(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Checkbox*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Checkbox>(thisVal);
builder.fontSize(jac::ValueWeak(ctx_, argv[0]).to<float>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue checked(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Checkbox*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Checkbox>(thisVal);
builder.checked(jac::ValueWeak(ctx_, argv[0]).to<bool>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue color(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Checkbox*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Checkbox>(thisVal);
builder.color(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

static JSValue text(JSContext* ctx_, JSValueConst thisVal, int argc, JSValueConst* argv) {
auto& builder = *reinterpret_cast<gridui::builder::Checkbox*>(JS_GetOpaque(thisVal, 1));
auto& builder = builderOpaque<gridui::builder::Checkbox>(thisVal);
builder.text(jac::ValueWeak(ctx_, argv[0]).to<std::string>());
return JS_DupValue(ctx_, thisVal);
}

public:
static jac::Object proto(jac::ContextRef ctx) {
static JSCFunction *getPropFunc(const AtomString& name) {
using namespace gridui;

auto proto = jac::Object::create(ctx);
if(name == "css") return builderCss<builder::Checkbox>;
if(name == "finish") return builderFinish<WidgetTypeId::Checkbox, builder::Checkbox, Checkbox>;

proto.set("fontSize", jac::Value(ctx, JS_NewCFunction(ctx, fontSize, "fontSize", 1)));
proto.set("checked", jac::Value(ctx, JS_NewCFunction(ctx, checked, "checked", 1)));
proto.set("color", jac::Value(ctx, JS_NewCFunction(ctx, color, "color", 1)));
proto.set("text", jac::Value(ctx, JS_NewCFunction(ctx, text, "text", 1)));
if(name == "fontSize") return fontSize;
if(name == "checked") return checked;
if(name == "color") return color;
if(name == "text") return text;

defineBuilderCallback<builder::Checkbox, Checkbox, &builder::Checkbox::onChanged>(ctx, proto, "onChanged");
if(name == "onChanged") return &builderCallbackImpl<builder::Checkbox, Checkbox, &builder::Checkbox::onChanged>;

return proto;
return nullptr;
}
};

Expand Down
Loading

0 comments on commit 45f6651

Please sign in to comment.