Skip to content

Commit 8cd5b68

Browse files
authored
Merge pull request #12 from wechat-miniprogram/feat-ffi-impl
feat: better ffi error impl
2 parents 69c8c6c + a4793ec commit 8cd5b68

File tree

8 files changed

+964
-311
lines changed

8 files changed

+964
-311
lines changed

float-pigment-css-macro/src/style_syntax.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,13 +894,13 @@ impl ToTokens for StyleSyntaxDefinition {
894894
writeln!(&mut style_doc, "| ---- | ---- | ---- |").unwrap();
895895
let table_list_a = supported_properties
896896
.iter()
897-
.filter(|x| !x.name.as_ref().unwrap().starts_with("-"));
897+
.filter(|x| !x.name.as_ref().unwrap().starts_with('-'));
898898
let table_list_b = supported_properties
899899
.iter()
900-
.filter(|x| x.name.as_ref().unwrap().starts_with("-"));
900+
.filter(|x| x.name.as_ref().unwrap().starts_with('-'));
901901
for x in table_list_a.chain(table_list_b) {
902902
let name = x.name.as_ref().unwrap();
903-
let non_standard = name.starts_with("-");
903+
let non_standard = name.starts_with('-');
904904
let name_col = if non_standard {
905905
format!("*`{}`*", name)
906906
} else {

float-pigment-css/float_pigment_css.h

Lines changed: 116 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,28 @@ enum class ContainKeyword {
8989
Paint,
9090
};
9191

92+
enum class FfiErrorCode {
93+
None,
94+
ThisNullPointer,
95+
PathNullPointer,
96+
PrefixNullPointer,
97+
SourceNullPointer,
98+
BufferNullPointer,
99+
ExprPtrNullPointer,
100+
StrNullPointer,
101+
InlineStyleTextNullPointer,
102+
InlineRuleNullPointer,
103+
StyleTextNullPointer,
104+
SelectorTextNullPointer,
105+
InvalidPath,
106+
JsonNullPointer,
107+
ArrayNullPointer,
108+
SelectorNullPointer,
109+
StyleSheetNullPointer,
110+
MapNullPointer,
111+
Unknown,
112+
};
113+
92114
enum class FontDisplay {
93115
Auto,
94116
Block,
@@ -265,6 +287,14 @@ enum class WarningKind : uint32_t {
265287
InvalidEnvDefaultValue,
266288
};
267289

290+
using NullPtr = const void*;
291+
292+
template<typename T>
293+
struct FfiResult {
294+
T value;
295+
FfiErrorCode err;
296+
};
297+
268298
struct StrRef {
269299
size_t offset;
270300
size_t len;
@@ -286,10 +316,6 @@ struct Warning {
286316
uint32_t end_col;
287317
};
288318

289-
struct CParserHooksContext {
290-
void *inner;
291-
};
292-
293319
template<typename T>
294320
struct Box {
295321
T *ptr;
@@ -9007,143 +9033,141 @@ struct StyleSheet {
90079033
};
90089034
};
90099035

9010-
struct StyleSheetImportIndexPtr {
9011-
void *ptr;
9012-
void *map;
9013-
};
9036+
using RawMutPtr = void*;
90149037

9015-
struct StyleSheetResourcePtr {
9016-
void *ptr;
9038+
struct CParserHooksContext {
9039+
void *inner;
90179040
};
90189041

90199042
struct CParserHooks {
90209043
void (*parsed_property)(CParserHooksContext, Property*);
90219044
};
90229045

9046+
using CustomPropertyGetter = const char*(*)(void *map, const char *name);
9047+
9048+
using CustomPropertySetter = void(*)(void *map, const char *name, const char *value);
9049+
90239050

90249051
extern "C" {
90259052

9026-
void array_str_ref_free(Array<StrRef> *x);
9053+
FfiResult<NullPtr> FPArrayStrRefFree(Array<StrRef> *x);
90279054

9028-
void array_warning_free(Array<Warning> *warnings);
9055+
FfiResult<NullPtr> FPArrayWarningFree(Array<Warning> *warnings);
90299056

9030-
void buffer_free(uint8_t *buffer_ptr, size_t buffer_len);
9057+
FfiResult<NullPtr> FPBufferFree(uint8_t *buffer_ptr, size_t buffer_len);
90319058

9032-
StrRef *css_parser_version();
9059+
FfiResult<StrRef*> FPCssParserVersion();
90339060

9034-
void generate_warning(CParserHooksContext *self, const char *message);
9061+
FfiResult<NullPtr> FPInlineStyleFree(InlineRule *inline_rule);
90359062

9036-
void inline_style_free(InlineRule *inline_rule);
9063+
FfiResult<ColorValue> FPParseColorFromString(const char *source);
90379064

9038-
ColorValue parse_color_from_string(const char *source);
9065+
FfiResult<InlineRule*> FPParseInlineStyle(const char *inline_style_text_ptr,
9066+
Array<Warning> **warnings);
90399067

9040-
InlineRule *parse_inline_style(const char *inline_style_text_ptr, Array<Warning> **warnings);
9068+
FfiResult<Selector*> FPParseSelectorFromString(const char *selector_text_ptr);
90419069

9042-
Selector *parse_selector_from_string(const char *selector_text_ptr);
9070+
FfiResult<StyleSheet*> FPParseStyleSheetFromString(const char *style_text_ptr);
90439071

9044-
StyleSheet *parse_style_sheet_from_string(const char *style_text_ptr);
9072+
FfiResult<NullPtr> FPSelectorFree(Selector *selector);
90459073

9046-
void selector_free(Selector *selector);
9074+
FfiResult<NullPtr> FPStrFree(const char *ptr);
90479075

9048-
void str_free(const char *ptr);
9076+
FfiResult<StrRef*> FPStyleSheetBincodeVersion(uint8_t *buffer_ptr, size_t buffer_len);
90499077

9050-
size_t str_len(const StrRef *self);
9078+
FfiResult<NullPtr> FPStyleSheetFree(StyleSheet *style_sheet);
90519079

9052-
const uint8_t *str_ptr(const StrRef *self);
9080+
FfiResult<NullPtr> FPStyleSheetImportIndexAddBincode(RawMutPtr this_,
9081+
const char *path,
9082+
uint8_t *buffer_ptr,
9083+
size_t buffer_len,
9084+
void (*drop_fn)(RawMutPtr),
9085+
RawMutPtr drop_args,
9086+
Array<Warning> **warnings);
90539087

9054-
StrRef *style_sheet_bincode_version(uint8_t *buffer_ptr, size_t buffer_len);
9088+
FfiResult<RawMutPtr> FPStyleSheetImportIndexDeserializeBincode(uint8_t *buffer_ptr,
9089+
size_t buffer_len,
9090+
void (*drop_fn)(RawMutPtr),
9091+
RawMutPtr drop_args);
90559092

9056-
void style_sheet_free(StyleSheet *style_sheet);
9093+
FfiResult<RawMutPtr> FPStyleSheetImportIndexDeserializeJson(const char *json);
90579094

9058-
void style_sheet_import_index_add_bincode(StyleSheetImportIndexPtr *this_,
9059-
const char *path,
9060-
uint8_t *buffer_ptr,
9061-
size_t buffer_len,
9062-
void (*drop_fn)(void*),
9063-
void *drop_args,
9064-
Array<Warning> **warnings);
9095+
FfiResult<NullPtr> FPStyleSheetImportIndexFree(RawMutPtr this_);
90659096

9066-
StyleSheetImportIndexPtr style_sheet_import_index_deserialize_bincode(uint8_t *buffer_ptr,
9067-
size_t buffer_len,
9068-
void (*drop_fn)(void*),
9069-
void *drop_args);
9097+
FfiResult<StyleSheet*> FPStyleSheetImportIndexGetStyleSheet(RawMutPtr this_, const char *path);
90709098

9071-
StyleSheetImportIndexPtr style_sheet_import_index_deserialize_json(const char *json);
9099+
FfiResult<Array<StrRef>*> FPStyleSheetImportIndexListDependencies(RawMutPtr this_,
9100+
const char *path);
90729101

9073-
void style_sheet_import_index_free(StyleSheetImportIndexPtr *this_);
9102+
FfiResult<Array<StrRef>*> FPStyleSheetImportIndexListDependency(RawMutPtr this_, const char *path);
90749103

9075-
StyleSheet *style_sheet_import_index_get_style_sheet(StyleSheetImportIndexPtr *this_,
9076-
const StrRef *path);
9104+
FfiResult<NullPtr> FPStyleSheetImportIndexMergeBincode(RawMutPtr this_,
9105+
uint8_t *buffer_ptr,
9106+
size_t buffer_len,
9107+
void (*drop_fn)(void*),
9108+
void *drop_args);
90779109

9078-
Array<StrRef> *style_sheet_import_index_list_dependencies(StyleSheetImportIndexPtr *this_,
9079-
const char *path);
9110+
FfiResult<RawMutPtr> FPStyleSheetImportIndexNew();
90809111

9081-
Array<StrRef> *style_sheet_import_index_list_dependency(StyleSheetImportIndexPtr *this_,
9082-
const char *path);
9112+
FfiResult<Array<StrRef>*> FPStyleSheetImportIndexQueryAndMarkDependencies(RawMutPtr this_,
9113+
const char *path);
90839114

9084-
void style_sheet_import_index_merge_bincode(StyleSheetImportIndexPtr *this_,
9085-
uint8_t *buffer_ptr,
9086-
size_t buffer_len,
9087-
void (*drop_fn)(void*),
9088-
void *drop_args);
9115+
FfiResult<NullPtr> FPStyleSheetImportIndexRemoveBincode(RawMutPtr this_, const char *path);
90899116

9090-
StyleSheetImportIndexPtr style_sheet_import_index_new();
9117+
FfiResult<uint8_t*> FPStyleSheetImportIndexSerializeBincode(RawMutPtr this_,
9118+
size_t *ret_buffer_len);
90919119

9092-
Array<StrRef> *style_sheet_import_index_query_and_mark_dependencies(StyleSheetImportIndexPtr *this_,
9093-
const char *path);
9120+
FfiResult<uint8_t*> FPStyleSheetImportIndexSerializeJson(RawMutPtr this_, size_t *ret_buffer_len);
90949121

9095-
void style_sheet_import_index_remove_bincode(StyleSheetImportIndexPtr *this_, const char *path);
9122+
FfiResult<NullPtr> FPStyleSheetResourceAddBincode(RawMutPtr this_,
9123+
const char *path,
9124+
uint8_t *buffer_ptr,
9125+
size_t buffer_len,
9126+
void (*drop_fn)(RawMutPtr),
9127+
RawMutPtr drop_args,
9128+
Array<Warning> **warnings);
90969129

9097-
uint8_t *style_sheet_import_index_serialize_bincode(StyleSheetImportIndexPtr *this_,
9098-
size_t *ret_buffer_len);
9130+
FfiResult<NullPtr> FPStyleSheetResourceAddSource(RawMutPtr this_,
9131+
const char *path,
9132+
const char *source,
9133+
Array<Warning> **warnings);
90999134

9100-
uint8_t *style_sheet_import_index_serialize_json(StyleSheetImportIndexPtr *this_,
9101-
size_t *ret_buffer_len);
9135+
FfiResult<NullPtr> FPStyleSheetResourceAddSourceWithHooks(RawMutPtr this_,
9136+
CParserHooks hooks,
9137+
const char *path,
9138+
const char *source,
9139+
Array<Warning> **warnings);
91029140

9103-
void style_sheet_resource_add_bincode(StyleSheetResourcePtr *this_,
9104-
const char *path,
9105-
uint8_t *buffer_ptr,
9106-
size_t buffer_len,
9107-
void (*drop_fn)(void*),
9108-
void *drop_args,
9109-
Array<Warning> **warnings);
9141+
FfiResult<NullPtr> FPStyleSheetResourceAddTagNamePrefix(RawMutPtr this_,
9142+
const char *path,
9143+
const char *prefix);
91109144

9111-
void style_sheet_resource_add_source(StyleSheetResourcePtr *this_,
9112-
const char *path,
9113-
const char *source,
9114-
Array<Warning> **warnings);
9145+
FfiResult<Array<StrRef>*> FPStyleSheetResourceDirectDependencies(RawMutPtr this_, const char *path);
91159146

9116-
void style_sheet_resource_add_source_with_hooks(StyleSheetResourcePtr *this_,
9117-
CParserHooks hooks,
9118-
const char *path,
9119-
const char *source,
9120-
Array<Warning> **warnings);
9147+
FfiResult<NullPtr> FPStyleSheetResourceFree(RawMutPtr this_);
91219148

9122-
void style_sheet_resource_add_tag_name_prefix(StyleSheetResourcePtr *this_,
9123-
const char *path,
9124-
const char *prefix);
9149+
FfiResult<RawMutPtr> FPStyleSheetResourceGenerateImportIndex(RawMutPtr this_);
91259150

9126-
Array<StrRef> *style_sheet_resource_direct_dependencies(StyleSheetResourcePtr *this_,
9127-
const char *path);
9151+
FfiResult<RawMutPtr> FPStyleSheetResourceNew();
91289152

9129-
void style_sheet_resource_free(StyleSheetResourcePtr *this_);
9153+
FfiResult<uint8_t*> FPStyleSheetResourceSerializeBincode(RawMutPtr this_,
9154+
const char *path,
9155+
size_t *ret_buffer_len);
91309156

9131-
StyleSheetImportIndexPtr style_sheet_resource_generate_import_index(StyleSheetResourcePtr *this_);
9157+
FfiResult<uint8_t*> FPStyleSheetResourceSerializeJson(RawMutPtr this_,
9158+
const char *path,
9159+
size_t *ret_buffer_len);
91329160

9133-
StyleSheetResourcePtr style_sheet_resource_new();
9161+
FfiResult<const char*> FPSubstituteVariable(const char *expr_ptr,
9162+
RawMutPtr map,
9163+
CustomPropertyGetter getter,
9164+
CustomPropertySetter setter);
91349165

9135-
uint8_t *style_sheet_resource_serialize_bincode(StyleSheetResourcePtr *this_,
9136-
const char *path,
9137-
size_t *ret_buffer_len);
9166+
FfiResult<NullPtr> generate_warning(CParserHooksContext *self, const char *message);
91389167

9139-
uint8_t *style_sheet_resource_serialize_json(StyleSheetResourcePtr *this_,
9140-
const char *path,
9141-
size_t *ret_buffer_len);
9168+
size_t str_len(const StrRef *self);
91429169

9143-
const char *substitute_variable(const char *expr_ptr,
9144-
void *map,
9145-
CustomPropertyGetter getter,
9146-
CustomPropertySetter setter);
9170+
const uint8_t *str_ptr(const StrRef *self);
91479171

91489172
} // extern "C"
91499173

0 commit comments

Comments
 (0)