use consturctor_def and method_def with hash

This commit is contained in:
pikastech 2022-10-15 18:02:43 +08:00
parent c6f6589585
commit 0aac8300f1
4 changed files with 26 additions and 11 deletions

View File

@ -425,6 +425,7 @@ const MethodProp floatMethod = {
#define class_def(_class) const Arg _class##Collect[] =
#define class_inhert(_class, _super) \
extern const NativeProperty _super##NativeProp; \
const NativeProperty _class##NativeProp = { \
.super = &_super##NativeProp, \
.methodGroup = _class##Collect, \

View File

@ -29,5 +29,4 @@
#define __TYNYOBJ__H
#include "PikaObj.h"
PikaObj* New_TinyObj(Args* args);
extern const NativeProperty TinyObjNativeProp;
#endif

View File

@ -196,6 +196,19 @@ impl ClassInfo {
pub fn new_class_fn(&self) -> String {
let mut new_class_fn = String::new();
let new_class_fn_head = format!("{}{{\n", self.new_class_fn_name());
let class_def = format!("class_def({}){{\n", self.this_class_name);
new_class_fn.push_str(&class_def);
for (_, method_info) in self.method_list.iter() {
new_class_fn.push_str(&method_info.get_define());
}
new_class_fn.push_str(&"};\n");
let class_inhert = format!("class_inhert({}, {});\n\n", self.this_class_name, self.super_class_name);
new_class_fn.push_str(&class_inhert);
new_class_fn.push_str(&new_class_fn_head);
let derive = format!(" PikaObj *self = New_{}(args);\n", self.super_class_name);
new_class_fn.push_str(&derive);
@ -204,9 +217,8 @@ impl ClassInfo {
new_class_fn.push_str(&object_info.new_object_fn());
}
for (_, method_info) in self.method_list.iter() {
new_class_fn.push_str(&method_info.get_define());
}
let obj_set_class = format!(" obj_setClass(self, {});\n", self.this_class_name);
new_class_fn.push_str(&obj_set_class);
new_class_fn.push_str(" return self;\n");
new_class_fn.push_str("}\n");

View File

@ -13,6 +13,10 @@ pub struct MethodInfo {
pub decorator_list: Vec<Decorator>,
}
pub fn hash_time33(key: &String) -> usize {
key.chars().fold(5381 as usize, |hash, c| hash.wrapping_mul(33).wrapping_add(c as usize))
}
impl MethodInfo {
pub fn new(
class_name: &String,
@ -71,9 +75,9 @@ impl MethodInfo {
// Some(s) => format!("->{}", s.to_string()),
// None => String::from(""),
// };
let mut class_define_method = String::from("class_defineMethod");
let mut class_define_method = String::from("method_def");
if self.is_constructor {
class_define_method = String::from("class_defineConstructor");
class_define_method = String::from("constructor_def");
}
let mut define = String::from("");
@ -84,12 +88,11 @@ impl MethodInfo {
define.push_str(
format!(
" {}(self, \"{}\", \"{}\", \n {}_{}Method);\n",
" {}({}_{}, {}),\n",
class_define_method,
self.name,
self.get_arg_list_define(),
self.class_name,
self.name
self.name,
hash_time33(&self.name)
)
.as_str(),
);
@ -170,7 +173,7 @@ impl MethodInfo {
method_fn_impl.push_str(&return_impl);
method_fn_impl.push_str("}\n");
let typedef = format!(
"method_typedef(\n {}_{},\n \"{}\",\n \"{}\"\n);\n\n",
"method_typedef(\n {}_{},\n \"{}\", \"{}\"\n);\n\n",
self.class_name,
self.name,
self.name,