Skip to content

Commit

Permalink
feat(template-compiler): backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
SgLy committed Aug 22, 2024
1 parent c06b81e commit 9f01dd4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 70 deletions.
2 changes: 0 additions & 2 deletions glass-easel-template-compiler/glass_easel_template_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ void tmpl_group_set_extra_runtime_script(TmplGroup *self,
const uint8_t *content_buf,
size_t content_len);

void tmpl_group_set_filter_funcs(TmplGroup *self, const uint8_t *content_buf, size_t content_len);

int32_t tmpl_group_set_inline_script(TmplGroup *self,
const uint8_t *path_buf,
size_t path_len,
Expand Down
10 changes: 0 additions & 10 deletions glass-easel-template-compiler/src/cbinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,6 @@ impl TmplGroup {
0
}

#[no_mangle]
pub unsafe extern "C" fn tmpl_group_set_filter_funcs(
&mut self,
content_buf: &u8,
content_len: usize,
) {
let content = String::from_utf8_lossy(slice::from_raw_parts(content_buf, content_len));
self.inner_mut().set_filter_funcs(&content);
}

#[no_mangle]
pub unsafe extern "C" fn tmpl_group_get_runtime_string(&self) -> StrRef {
self.inner().get_runtime_string().into()
Expand Down
68 changes: 26 additions & 42 deletions glass-easel-template-compiler/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ var D = (() => {
})()
"#;

fn runtime_fns<W: std::fmt::Write>(
w: &mut JsFunctionScopeWriter<W>,
need_wxs_runtime: bool,
) -> Result<(), TmplError> {
for (k, v) in RUNTIME_ITEMS.iter() {
w.expr_stmt(|w| {
write!(w, "var {}={}", k, v)?;
Ok(())
})?;
}
if need_wxs_runtime {
w.expr_stmt(|w| {
write!(w, "{}", WXS_RUNTIME)?;
Ok(())
})?;
w.expr_stmt(|w| {
write!(w, "var Q={{A:(x)=>x,B:(x)=>x}}")?;
Ok(())
})?;
}
Ok(())
}

fn runtime_var_list() -> Vec<&'static str> {
RUNTIME_ITEMS.iter().map(|(k, _)| *k).collect()
}
Expand Down Expand Up @@ -127,7 +150,6 @@ pub struct TmplGroup {
has_scripts: bool,
extra_runtime_string: String,
dev_mode: bool,
filter_funcs: Option<String>,
}

impl TmplGroup {
Expand All @@ -139,7 +161,6 @@ impl TmplGroup {
has_scripts: false,
extra_runtime_string: String::new(),
dev_mode: false,
filter_funcs: None,
}
}

Expand Down Expand Up @@ -257,7 +278,7 @@ impl TmplGroup {
pub fn get_runtime_string(&self) -> String {
let mut w = JsTopScopeWriter::new(String::new());
w.function_scope(|w| {
self.runtime_fns(w)?;
runtime_fns(w, self.has_scripts)?;
Ok(())
})
.unwrap();
Expand Down Expand Up @@ -340,48 +361,11 @@ impl TmplGroup {
Ok(w.finish())
}

pub fn set_filter_funcs(&mut self, funcs: &str) {
self.filter_funcs = Some(funcs.to_string())
}

pub fn clear_filter_funcs(&mut self) {
self.filter_funcs = None
}

pub fn has_filter_funcs(&self) -> bool {
self.filter_funcs.is_some()
}

fn runtime_fns<W: std::fmt::Write>(
&self,
w: &mut JsFunctionScopeWriter<W>,
) -> Result<(), TmplError> {
for (k, v) in RUNTIME_ITEMS.iter() {
w.expr_stmt(|w| {
write!(w, "var {}={}", k, v)?;
Ok(())
})?;
}
if self.has_scripts {
w.expr_stmt(|w| {
write!(w, "{}", WXS_RUNTIME)?;
Ok(())
})?;
if let Some(filter_funcs) = &self.filter_funcs {
w.expr_stmt(|w| {
write!(w, "var Q={}", filter_funcs)?;
Ok(())
})?;
}
}
Ok(())
}

fn write_group_global_content(
&self,
w: &mut JsFunctionScopeWriter<String>,
) -> Result<(), TmplError> {
self.runtime_fns(w)?;
runtime_fns(w, self.has_scripts)?;
if self.extra_runtime_string.len() > 0 {
w.custom_stmt_str(&self.extra_runtime_string)?;
}
Expand Down Expand Up @@ -476,7 +460,7 @@ impl TmplGroup {
pub fn export_globals(&self) -> Result<String, TmplError> {
let mut w = JsTopScopeWriter::new(String::new());
w.function_scope(|w| {
self.runtime_fns(w)?;
runtime_fns(w, self.has_scripts)?;
if self.extra_runtime_string.len() > 0 {
w.custom_stmt_str(&self.extra_runtime_string)?;
}
Expand Down
5 changes: 0 additions & 5 deletions glass-easel-template-compiler/src/js_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ impl TmplGroup {
Ok(())
}

#[wasm_bindgen(js_name = "setFilterFuncs")]
pub fn set_filter_funcs(&mut self, s: &str) {
self.group.set_filter_funcs(s)
}

#[wasm_bindgen(js_name = "getRuntimeString")]
pub fn get_runtime_string(&self) -> String {
self.group.get_runtime_string()
Expand Down
6 changes: 5 additions & 1 deletion glass-easel-template-compiler/src/proc_gen/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ impl Template {
w.expr_stmt(|w| {
write!(w, "H[{key}]=", key = gen_lit_str(key))?;
w.function_args("R,C,D,U", |w| {
if has_scripts && group.has_filter_funcs() {
if has_scripts {
w.expr_stmt(|w| {
write!(w, "R.setFnFilter(Q.A,Q.B)")?;
Ok(())
})?;
w.expr_stmt(|w| {
write!(w, "R.setEventListenerWrapper(Q.C)")?;
Ok(())
})?;
}
let mut writer = JsTopScopeWriter::new(String::new());
writer.align(w);
Expand Down
13 changes: 11 additions & 2 deletions glass-easel/src/tmpl/proc_gen_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,8 +1158,17 @@ export class ProcGenWrapper {
}
}

/** @deprecated */
setFnFilter() {}
// set change properties filter
setFnFilter(changePropFilter: ChangePropFilter) {
this.changePropFilter = changePropFilter
}

// set event listener wrapper
setEventListenerWrapper(eventListenerWrapper?: EventListenerWrapper) {
if (typeof eventListenerWrapper === 'function') {
this.eventListenerWrapper = eventListenerWrapper
}
}

// get dev args object
devArgs(elem: Element): TmplDevArgs {
Expand Down
17 changes: 10 additions & 7 deletions glass-easel/tests/base/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,22 @@ type TemplateOptions = {

type FilterFuncs = {
A?: glassEasel.template.ChangePropFilter
B?: glassEasel.template.EventListenerWrapper
C?: glassEasel.template.EventListenerWrapper
}

export const tmpl = (src: string, options?: TemplateOptions, filterFuncs?: FilterFuncs) => {
const group = TmplGroup.newDev()
if (filterFuncs !== undefined) {
const A = filterFuncs.A || (() => {})
const B = filterFuncs.B || (() => {})
group.setFilterFuncs(`{A:${A.toString()},B:${B.toString()}}`)
}
group.addTmpl('', src)
const genObjectSrc = `return ${group.getTmplGenObjectGroups()}`
let genObjectSrc = `return ${group.getTmplGenObjectGroups()}`
group.free()
if (filterFuncs !== undefined) {
const A = filterFuncs.A || ((x) => x)
const C = filterFuncs.C || 'undefined'
genObjectSrc = genObjectSrc.replace(
'var Q={A:(x)=>x,B:(x)=>x}',
`var Q={A:${A.toString()},C:${C.toString()}}`,
)
}
// console.info(genObjectSrc)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const genObjectGroupList = new Function(genObjectSrc)() as { [key: string]: any }
Expand Down
2 changes: 1 addition & 1 deletion glass-easel/tests/tmpl/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const testCases = (testBackend: glassEasel.GeneralBackendContext) => {
`,
{},
{
B: (elem, event, listener) => {
C: (elem, event, listener) => {
switch (event.getEventName()) {
case 'customEv': {
event.target = Object.assign(event.target, { id: 'b' })
Expand Down

0 comments on commit 9f01dd4

Please sign in to comment.