|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#define CAML_NAME_SPACE |
| 9 | + |
| 10 | +#include <caml/alloc.h> |
| 11 | +#include <caml/mlvalues.h> |
| 12 | +#include <caml/memory.h> |
| 13 | +#include <caml/callback.h> |
| 14 | + |
| 15 | +namespace flowparser { |
| 16 | + |
| 17 | +static value list_head(value props) { |
| 18 | + return Field(props, 0); |
| 19 | +} |
| 20 | + |
| 21 | +static bool list_has_next(value props) { |
| 22 | + return props != Val_emptylist; |
| 23 | +} |
| 24 | + |
| 25 | +static value list_tail(value props) { |
| 26 | + return Field(props, 1); |
| 27 | +} |
| 28 | + |
| 29 | +static char* get_prop_key(value prop) { |
| 30 | + return String_val(Field(prop, 0)); |
| 31 | +} |
| 32 | + |
| 33 | +static value get_prop_value(value prop) { |
| 34 | + return Field(prop, 1); |
| 35 | +} |
| 36 | + |
| 37 | +template <class T> |
| 38 | +class AbstractTranslator { |
| 39 | +public: |
| 40 | + virtual T convert_string(char *str) = 0; |
| 41 | + virtual T convert_number(double n) = 0; |
| 42 | + virtual T convert_bool(long b) = 0; |
| 43 | + virtual T convert_null() = 0; |
| 44 | + virtual T convert_undefined() = 0; |
| 45 | + virtual T convert_object(value props) = 0; |
| 46 | + virtual T convert_array(value items) = 0; |
| 47 | + |
| 48 | + T convert_json(value v) { |
| 49 | + if (Is_long(v)) { |
| 50 | + if (Long_val(v) == 0) { |
| 51 | + return convert_null(); |
| 52 | + } |
| 53 | + // no other immediate values should exist |
| 54 | + return convert_undefined(); // TODO: raise v8 exception |
| 55 | + }; |
| 56 | + switch (Tag_val(v)) { |
| 57 | + // JObject |
| 58 | + case 0: |
| 59 | + return convert_object(Field(v, 0)); |
| 60 | + |
| 61 | + // JArray |
| 62 | + case 1: |
| 63 | + return convert_array(Field(v, 0)); |
| 64 | + |
| 65 | + // JString |
| 66 | + case 2: |
| 67 | + return convert_string(String_val(Field(v, 0))); |
| 68 | + |
| 69 | + // JNumber |
| 70 | + case 3: |
| 71 | + return convert_number(Double_val(Field(v, 0))); |
| 72 | + |
| 73 | + // JBool |
| 74 | + case 4: |
| 75 | + return convert_bool(Long_val(Field(v, 0))); |
| 76 | + |
| 77 | + default: |
| 78 | + // no other tags exist! |
| 79 | + return convert_undefined(); // TODO: raise v8 exception |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + T parse(const char *content) { |
| 84 | + CAMLparam0(); |
| 85 | + CAMLlocal2(content_val, result_val); |
| 86 | + |
| 87 | + static value * func = NULL; |
| 88 | + if (func == NULL) { |
| 89 | + func = caml_named_value("flow_parse"); |
| 90 | + } |
| 91 | + |
| 92 | + content_val = caml_copy_string(content); |
| 93 | + result_val = caml_callback(*func, content_val); |
| 94 | + |
| 95 | + CAMLreturnT(T, convert_object(Field(result_val, 0))); |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +void init() { |
| 100 | + char *argv[] = {NULL}; |
| 101 | + caml_startup(argv); |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace flow |
0 commit comments