Advertisement
Xliff

NativeCall and nativecast walk into a bar....

Apr 3rd, 2016
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 22.88 KB | None | 0 0
  1. use NativeCall;
  2.  
  3. constant XQC_VERSION_NUMBER = 1;
  4.  
  5. ## Enumerations
  6.  
  7. enum XQC_Error is export (
  8.    XQC_NO_ERROR => 0,
  9.    XQC_END_OF_SEQUENCE => 1,
  10.    XQC_NO_CURRENT_ITEM => 2,
  11.    XQC_PARSE_ERROR => 3,
  12.    XQC_INVALID_ARGUMENT => 4,
  13.    XQC_NOT_NODE => 5,
  14.    XQC_INTERNAL_ERROR => 6,
  15.    XQC_NOT_IMPLEMENTED => 7,
  16.    XQC_UNRECOGNIZED_ENCODING => 8,
  17.    XQC_STATIC_ERROR => 9,
  18.    XQC_TYPE_ERROR => 10,
  19.    XQC_DYNAMIC_ERROR => 11,
  20.    XQC_SERIALIZATION_ERROR => 12
  21. );
  22.  
  23. enum XQC_ItemType is export (
  24.    XQC_EMPTY_TYPE => 0,
  25.    XQC_DOCUMENT_TYPE => 1,
  26.    XQC_ELEMENT_TYPE => 2,
  27.    XQC_ATTRIBUTE_TYPE => 3,
  28.    XQC_TEXT_TYPE => 4,
  29.    XQC_PROCESSING_INSTRUCTION_TYPE => 5,
  30.    XQC_COMMENT_TYPE => 6,
  31.    XQC_NAMESPACE_TYPE => 7,
  32.    XQC_ANY_SIMPLE_TYPE => 8,
  33.    XQC_ANY_URI_TYPE => 9,
  34.    XQC_BASE_64_BINARY_TYPE => 10,
  35.    XQC_BOOLEAN_TYPE => 11,
  36.    XQC_DATE_TYPE => 12,
  37.    XQC_DATE_TIME_TYPE => 13,
  38.    XQC_DAY_TIME_DURATION_TYPE => 14,
  39.    XQC_DECIMAL_TYPE => 15,
  40.    XQC_DOUBLE_TYPE => 16,
  41.    XQC_DURATION_TYPE => 17,
  42.    XQC_FLOAT_TYPE => 18,
  43.    XQC_G_DAY_TYPE => 19,
  44.    XQC_G_MONTH_TYPE => 20,
  45.    XQC_G_MONTH_DAY_TYPE => 21,
  46.    XQC_G_YEAR_TYPE => 22,
  47.    XQC_G_YEAR_MONTH_TYPE => 23,
  48.    XQC_HEX_BINARY_TYPE => 24,
  49.    XQC_NOTATION_TYPE => 25,
  50.    XQC_QNAME_TYPE => 26,
  51.    XQC_STRING_TYPE => 27,
  52.    XQC_TIME_TYPE => 28,
  53.    XQC_UNTYPED_ATOMIC_TYPE => 29,
  54.    XQC_YEAR_MONTH_DURATION_TYPE => 30
  55. );
  56.  
  57. enum XQC_XPath1Mode is export (
  58.    XQC_XPATH2_0 => 0,
  59.    XQC_XPATH1_0 => 1
  60. );
  61.  
  62. enum XQC_OrderingMode is export (
  63.    XQC_ORDERED => 0,
  64.    XQC_UNORDERED => 1
  65. );
  66.  
  67. enum XQC_OrderEmptyMode is export (
  68.    XQC_EMPTY_GREATEST => 0,
  69.    XQC_EMPTY_LEAST => 1
  70. );
  71.  
  72. enum XQC_InheritMode is export (
  73.    XQC_INHERIT_NS => 0,
  74.    XQC_NO_INHERIT_NS => 1
  75. );
  76.  
  77. enum XQC_PreserveMode is export (
  78.    XQC_PRESERVE_NS => 0,
  79.    XQC_NO_PRESERVE_NS => 1
  80. );
  81.  
  82. enum XQC_BoundarySpaceMode is export (
  83.    XQC_PRESERVE_SPACE => 0,
  84.    XQC_STRIP_SPACE => 1
  85. );
  86.  
  87. enum XQC_ConstructionMode is export (
  88.    XQC_PRESERVE_CONS => 0,
  89.    XQC_STRIP_CONS => 1
  90. );
  91. ## Structures
  92.  
  93.  
  94. # == /usr/include/xqc.h ==
  95.  
  96. class XQC_InputStream_s is repr<CStruct> {
  97.     has Str                           $.encoding; # const char* encoding
  98.     has Pointer                       $.user_data; # void* user_data
  99.     has Pointer                       $!read; # F:unsigned int ( Typedef<XQC_InputStream>->|XQC_InputStream_s|*, void*, unsigned int)* read
  100.     has Pointer                       $!free; # F:void ( Typedef<XQC_InputStream>->|XQC_InputStream_s|*)* free
  101.    
  102.     method read(str $b, Int $v where * > 0) {
  103.         my &func_sig = nativecast(:(XQC_InputStream_s, Pointer, uint32), $!read);
  104.  
  105.         return func_sig(self, $b, $v);
  106.     }
  107.  
  108.     submethod DESTROY {
  109.         $!free();
  110.     }
  111. }
  112.  
  113. class XQC_ErrorHandler_s is repr<CStruct> is export {
  114.     has Pointer                       $.user_data; # void* user_data
  115.     has Pointer                       $.error; # F:void ( Typedef<XQC_ErrorHandler>->|XQC_ErrorHandler_s|*, XQC_Error, const char*, const char*, const char*, Typedef<XQC_Sequence>->|XQC_Sequence_s|*)* error
  116. }
  117.  
  118.  
  119. class XQC_StaticContext_s is repr<CStruct> is export {
  120.     has Pointer                       $.create_child_context; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, Typedef<XQC_StaticContext>->|XQC_StaticContext_s|**)* create_child_context
  121.     has Pointer                       $.declare_ns; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char*, const char*)* declare_ns
  122.     has Pointer                       $.get_ns_by_prefix; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char*, const char**)* get_ns_by_prefix
  123.     has Pointer                       $.set_default_element_and_type_ns; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char*)* set_default_element_and_type_ns
  124.     has Pointer                       $.get_default_element_and_type_ns; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char**)* get_default_element_and_type_ns
  125.     has Pointer                       $.set_default_function_ns; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char*)* set_default_function_ns
  126.     has Pointer                       $.get_default_function_ns; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char**)* get_default_function_ns
  127.     has Pointer                       $.set_xpath_compatib_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_XPath1Mode)* set_xpath_compatib_mode
  128.     has Pointer                       $.get_xpath_compatib_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_XPath1Mode*)* get_xpath_compatib_mode
  129.     has Pointer                       $.set_construction_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_ConstructionMode)* set_construction_mode
  130.     has Pointer                       $.get_construction_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_ConstructionMode*)* get_construction_mode
  131.     has Pointer                       $.set_ordering_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_OrderingMode)* set_ordering_mode
  132.     has Pointer                       $.get_ordering_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_OrderingMode*)* get_ordering_mode
  133.     has Pointer                       $.set_default_order_empty_sequences; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_OrderEmptyMode)* set_default_order_empty_sequences
  134.     has Pointer                       $.get_default_order_empty_sequences; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_OrderEmptyMode*)* get_default_order_empty_sequences
  135.     has Pointer                       $.set_boundary_space_policy; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_BoundarySpaceMode)* set_boundary_space_policy
  136.     has Pointer                       $.get_boundary_space_policy; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_BoundarySpaceMode*)* get_boundary_space_policy
  137.     has Pointer                       $.set_copy_ns_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_PreserveMode, XQC_InheritMode)* set_copy_ns_mode
  138.     has Pointer                       $.get_copy_ns_mode; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, XQC_PreserveMode*, XQC_InheritMode*)* get_copy_ns_mode
  139.     has Pointer                       $.set_base_uri; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char*)* set_base_uri
  140.     has Pointer                       $.get_base_uri; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char**)* get_base_uri
  141.     has Pointer                       $.set_error_handler; # F:XQC_Error ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, Typedef<XQC_ErrorHandler>->|XQC_ErrorHandler_s|*)* set_error_handler
  142.     has Pointer                       $.get_error_handler; # F:XQC_Error ( const Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, Typedef<XQC_ErrorHandler>->|XQC_ErrorHandler_s|**)* get_error_handler
  143.     has Pointer                       $.get_interface; # F:void* ( const Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, const char*)* get_interface
  144.     has Pointer                       $.free; # F:void ( Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*)* free
  145. }
  146. class XQC_Expression_s is repr<CStruct> is export {
  147.     has Pointer                       $.create_context; # F:XQC_Error ( const Typedef<XQC_Expression>->|XQC_Expression_s|*, Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|**)* create_context
  148.     has Pointer                       $.execute; # F:XQC_Error ( const Typedef<XQC_Expression>->|XQC_Expression_s|*, const Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* execute
  149.     has Pointer                       $.get_interface; # F:void* ( const Typedef<XQC_Expression>->|XQC_Expression_s|*, const char*)* get_interface
  150.     has Pointer                       $!free; # F:void ( Typedef<XQC_Expression>->|XQC_Expression_s|*)* free
  151.  
  152.     submethod DESTROY {
  153.         $!free();
  154.     }
  155. }
  156.  
  157.  
  158. class XQC_Sequence_s is repr<CStruct> is export {
  159.     has Pointer                       $!next; # F:XQC_Error ( Typedef<XQC_Sequence>->|XQC_Sequence_s|*)* next
  160.     has Pointer                       $!item_type; # F:XQC_Error ( const Typedef<XQC_Sequence>->|XQC_Sequence_s|*, XQC_ItemType*)* item_type
  161.     has Pointer                       $!type_name; # F:XQC_Error ( const Typedef<XQC_Sequence>->|XQC_Sequence_s|*, const char**, const char**)* type_name
  162.     has Pointer                       $!string_value; # F:XQC_Error ( const Typedef<XQC_Sequence>->|XQC_Sequence_s|*, const char**)* string_value
  163.     has Pointer                       $!integer_value; # F:XQC_Error ( const Typedef<XQC_Sequence>->|XQC_Sequence_s|*, int*)* integer_value
  164.     has Pointer                       $!double_value; # F:XQC_Error ( const Typedef<XQC_Sequence>->|XQC_Sequence_s|*, double*)* double_value
  165.     has Pointer                       $!node_name; # F:XQC_Error ( const Typedef<XQC_Sequence>->|XQC_Sequence_s|*, const char**, const char**)* node_name
  166.     has Pointer                       $!get_interface; # F:void* ( const Typedef<XQC_Sequence>->|XQC_Sequence_s|*, const char*)* get_interface
  167.     has Pointer                       $!free; # F:void ( Typedef<XQC_Sequence>->|XQC_Sequence_s|*)* free
  168.  
  169.     method next {
  170.         my &func_sig = nativecast(:(XQC_Sequence_s --> XQC_Error), $!next);
  171.        
  172.         my $err = &func_sig(self);
  173.         testErr($err);
  174.     }
  175.  
  176.     method item_type {
  177.         my &func_sig = nativecast(:(XQC_Sequence_s, XQC_ItemType --> XQC_Error), $!item_type);
  178.        
  179.         my XQC_ItemType $it;
  180.         my $err = &func_sig(self, $it);
  181.         testErr($err);
  182.  
  183.         return $it;
  184.     }
  185.  
  186.     method string_value {
  187.         my &func_sig = nativecast(:(XQC_Sequence_s, Pointer --> XQC_Error), $!string_value);
  188.         my str $sval = str.new();
  189.  
  190.         my $err = &func_sig(self, $sval);
  191.         testErr($err);
  192.  
  193.         return $sval;
  194.     }
  195.  
  196.     method integer_value {
  197.         my &func_sig = nativecast(:(XQC_Sequence_s, Pointer --> XQC_Error), $!integer_value);
  198.         my Int $ival = Int.new();
  199.  
  200.         my $err = &func_sig(self, $ival);
  201.         testErr($err);
  202.  
  203.         return $ival;
  204.     }
  205.  
  206.  
  207.     method double_value {
  208.         my &func_sig = nativecast(:(XQC_Sequence_s, Pointer --> XQC_Error), $!double_value);
  209.         my Num $nval = Num.new();
  210.  
  211.         my $err = &func_sig(self, $nval);
  212.         testErr($err);
  213.  
  214.         return $nval;
  215.     }
  216.  
  217.     method node_name {
  218.         my &func_sig = nativecast(:(XQC_Sequence_s, Pointer, Pointer --> XQC_Error), $!node_name);
  219.         my str $uri = str.new();
  220.         my str $name = str.new();
  221.  
  222.         my $err = &func_sig(self, $uri, $name);
  223.         testErr($err);
  224.  
  225.         return ($uri, $name);
  226.     }
  227.  
  228.  
  229.     submethod DESTROY {
  230.         $!free();
  231.     }
  232. }
  233.  
  234.  
  235. class XQC_Implementation_s is repr<CStruct> is export {
  236.     has Pointer                       $!create_context; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, Typedef<XQC_StaticContext>->|XQC_StaticContext_s|**)* create_context
  237.     has Pointer                       $!prepare; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, const char*, const Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, Typedef<XQC_Expression>->|XQC_Expression_s|**)* prepare
  238.     has Pointer                       $!prepare_file; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, Typedef<FILE>->|_IO_FILE|*, const Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, Typedef<XQC_Expression>->|XQC_Expression_s|**)* prepare_file
  239.     has Pointer                       $!prepare_stream; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, Typedef<XQC_InputStream>->|XQC_InputStream_s|*, const Typedef<XQC_StaticContext>->|XQC_StaticContext_s|*, Typedef<XQC_Expression>->|XQC_Expression_s|**)* prepare_stream
  240.     has Pointer                       $!parse_document; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, const char*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* parse_document
  241.     has Pointer                       $!parse_document_file; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, Typedef<FILE>->|_IO_FILE|*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* parse_document_file
  242.     has Pointer                       $!parse_document_stream; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, Typedef<XQC_InputStream>->|XQC_InputStream_s|*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* parse_document_stream
  243.     has Pointer                       $!create_empty_sequence; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* create_empty_sequence
  244.     has Pointer                       $!create_singleton_sequence; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, XQC_ItemType, const char*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* create_singleton_sequence
  245.     has Pointer                       $!create_string_sequence; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, const char**, unsigned int, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* create_string_sequence
  246.     has Pointer                       $!create_integer_sequence; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, int*, unsigned int, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* create_integer_sequence
  247.     has Pointer                       $!create_double_sequence; # F:XQC_Error ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*, double*, unsigned int, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* create_double_sequence
  248.  
  249.     # cw: -YYY- Not implementing yet...
  250.     has Pointer                       $!get_interface; # F:void* ( const Typedef<XQC_Implementation>->|XQC_Implementation_s|*, const char*)* get_interface
  251.     has Pointer                       $!free; # F:void ( Typedef<XQC_Implementation>->|XQC_Implementation_s|*)* free
  252.  
  253.     #has XQC_StaticContext_s          $!context;
  254.  
  255.     sub new {
  256.         createXQillaXQCImplementation(XQC_VERSION_NUMBER);
  257.     }
  258.  
  259.  
  260.     # cw: Implemented for completeness, but I do not yet see a need for its
  261.     # use in the Perl6 methods, since passing 0 will give the default
  262.     # static context.
  263.     #method !createContext() {
  264.     #   my &func_sig = nativecast(:(XQC_Implementation_s, XQC_StaticContext_s --> XQC_Error), $!create_context);
  265.     #
  266.     #   $!context = XQC_StaticContext_s.new();
  267.     #
  268.     #   my $err = &func_sig(self, $!context);
  269.     #   testErr($err);
  270.     #}
  271.  
  272.     method prepare(str $expr) {
  273.         my &func_sig = nativecast(:(XQC_Implementation_s, str, Pointer, Pointer --> int32), $!prepare);
  274.         my $xpr = XQC_Expression_s.new();
  275.  
  276.         my $err = &func_sig(self, $expr, 0, $xpr);
  277.         testErr($err);
  278.        
  279.         return $xpr;
  280.     }
  281.  
  282.     multi method prepare_file(IO::Handle $fh) {
  283.         my &func_sig = nativecast(:(XQC_Implementation_s, Pointer, XQC_StaticContext_s, Pointer --> XQC_Error), $!prepare_file);
  284.         #createContext() unless $!context.defined;
  285.         my $NF = fdopen($fh.native-descriptor(), "r");
  286.         my $xpr = XQC_Expression_s.new();
  287.  
  288.         my $err = &func_sig(self, $NF, XQC_StaticContext_s, $xpr);
  289.         testErr($err);
  290.  
  291.         fdclose($NF);
  292.         close($fh);
  293.         free($NF);
  294.  
  295.         return $xpr;
  296.     }
  297.  
  298.     multi method prepare_file(str $file_name) {
  299.         die "File not found." unless $file_name.IO.e;
  300.         my $fh = open $file_name, :r;
  301.  
  302.         return .prepare_file($fh);
  303.     }
  304.  
  305.     method prepare_stream(XQC_InputStream_s $stream) {
  306.         my &func_sig = nativecast(:(XQC_Implementation_s, XQC_InputStream_s, XQC_StaticContext_s, Pointer --> XQC_Error), $!prepare_stream);
  307.         my $xpr = XQC_Expression_s.new();
  308.  
  309.         my $err = &func_sig(self, $stream, XQC_StaticContext_s, $xpr);
  310.         testErr($err);
  311.  
  312.         return $xpr;
  313.     }
  314.  
  315.     method parse_document(str $doc) {
  316.         my &func_sig = nativecast(:(XQC_Implementation_s, str, Pointer --> XQC_Error), $!parse_document);
  317.         my $seq = XQC_Sequence_s.new();
  318.  
  319.         my $err = &func_sig(self, $doc, $seq);
  320.         testErr($err);
  321.  
  322.         return $seq;
  323.     }
  324.  
  325.     multi method parse_document_file(str $file_name) {
  326.         die "File not found." unless $file_name.IO.e;
  327.         my $fh = open $file_name, :r;
  328.  
  329.         return .parse_document_file($fh);
  330.     }
  331.  
  332.     multi method parse_document_file(IO::Handle $fh) {
  333.         my &func_sig = nativecast(:(XQC_Implementation_s, Pointer, Pointer --> XQC_Error), $!parse_document_file);
  334.         my $seq = XQC_Sequence_s.new();
  335.  
  336.         my $NF = fdopen($fh.native-descriptor(), "r");
  337.         my $err = &func_sig(self, $NF, $seq);
  338.         testErr($err);
  339.  
  340.         fdclose($NF);
  341.         close($fh);
  342.         free($NF);
  343.  
  344.         return $seq;
  345.     }
  346.  
  347.     method parse_document_stream(XQC_InputStream_s $stream) {
  348.         my &func_sig = nativecast(:(XQC_Implementation_s, XQC_InputStream_s, Pointer --> XQC_Error), $!parse_document_stream);
  349.         my $seq = XQC_Sequence_s.new();
  350.  
  351.         my $err = &func_sig(self, $stream, $seq);
  352.         testErr($err);
  353.  
  354.         return $seq;
  355.     }
  356.  
  357.     method create_empty_sequence {
  358.         my &func_sig = nativecast(:(XQC_Implementation_s, Pointer --> XQC_Error), $!create_empty_sequence);
  359.         my $seq = XQC_Sequence_s.new();
  360.  
  361.         my $err = &func_sig(self, $seq);
  362.         testErr($err);
  363.  
  364.         return $seq;
  365.     }
  366.  
  367.     method create_singleton_sequence(str $val, XQC_ItemType $type = XQC_ANY_SIMPLE_TYPE) {
  368.         my &func_sig = nativecast(:(XQC_Implementation_s, XQC_ItemType, str, Pointer --> XQC_Error), $!create_singleton_sequence);
  369.         my $seq = XQC_Sequence_s.new();
  370.  
  371.         my $err = &func_sig(self, $type, $val, $seq);
  372.         testErr($err);
  373.  
  374.         return $seq;
  375.     }
  376.  
  377.     method create_string_sequence(Array $arr, Int $count?) {
  378.         my $idx = 0;
  379.         my $carr = CArray[Str].new();
  380.         for ($arr) -> $v {
  381.             die "Non string value encountered in create_string_sequence."
  382.                 unless $v ~~ Str;
  383.             $carr[$idx++] = $v;
  384.         }
  385.         $count ||= $idx;
  386.         if ($count != $idx) {
  387.             warn "Count of objects does not match parameter in create_string_sequence.";
  388.         }
  389.  
  390.         my &func_sig = nativecast(:(XQC_Implementation_s, CArray[Str], uint, Pointer --> XQC_Error), $!create_string_sequence);
  391.         my $seq = XQC_Sequence_s.new();
  392.  
  393.         my $err = &func_sig(self, $carr, $count, $seq);
  394.         testErr($err);
  395.  
  396.         return $seq;
  397.     }
  398.  
  399.     method create_integer_sequence(Array $arr, Int $count?) {
  400.         my $idx = 0;
  401.         my $carr = CArray[int32].new();
  402.         for ($arr) -> $v {
  403.             die "Non integer value or overflow encountered in create_integer_sequence."
  404.                 unless [&&] (
  405.                     $v ~~ Int,
  406.                     $v <= 2 ** 32 - 1,
  407.                     $v >= -(2 ** 32)
  408.                 );
  409.  
  410.             $carr[$idx++] = $v;
  411.         }
  412.         $count ||= $idx;
  413.         if ($count != $idx) {
  414.             warn "Count of objects does not match parameter in create_integer_sequence.";
  415.         }
  416.  
  417.         my &func_sig = nativecast(:(XQC_Implementation_s, CArray[int32], uint, Pointer --> XQC_Error), $!create_string_sequence);
  418.         my $seq = XQC_Sequence_s.new();
  419.  
  420.         my $err = &func_sig(self, $carr, $count, $seq);
  421.         testErr($err);
  422.  
  423.         return $seq;
  424.     }
  425.  
  426.     method create_double_sequence(Array $arr, Int $count?) {
  427.         my $idx = 0;
  428.         my $carr = CArray[num64].new();
  429.         for ($arr) -> $v {
  430.             die "Non double value encountered in create_double_sequence."
  431.                 unless $v ~~ Real;
  432.                
  433.  
  434.             $carr[$idx++] = $v;
  435.         }
  436.         $count ||= $idx;
  437.         if ($count != $idx) {
  438.             warn "Count of objects does not match parameter in create_double_sequence.";
  439.         }
  440.  
  441.         my &func_sig = nativecast(:(XQC_Implementation_s, CArray[num64], uint, Pointer --> XQC_Error), $!create_string_sequence);
  442.         my $seq = XQC_Sequence_s.new();
  443.  
  444.         my $err = &func_sig(self, $carr, $count, $seq);
  445.         testErr($err);
  446.  
  447.         return $seq;
  448.     }
  449.  
  450.  
  451.     submethod DESTROY {
  452.         $!free();
  453.     }
  454. }
  455.  
  456.  
  457. class XQC_DynamicContext_s is repr<CStruct> is export {
  458.     has Pointer                       $!set_variable; # F:XQC_Error ( Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, const char*, const char*, Typedef<XQC_Sequence>->|XQC_Sequence_s|*)* set_variable
  459.     has Pointer                       $!get_variable; # F:XQC_Error ( const Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, const char*, const char*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* get_variable
  460.     has Pointer                       $!set_context_item; # F:XQC_Error ( Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, Typedef<XQC_Sequence>->|XQC_Sequence_s|*)* set_context_item
  461.     has Pointer                       $!get_context_item; # F:XQC_Error ( const Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, Typedef<XQC_Sequence>->|XQC_Sequence_s|**)* get_context_item
  462.     has Pointer                       $!set_implicit_timezone; # F:XQC_Error ( Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, int)* set_implicit_timezone
  463.     has Pointer                       $!get_implicit_timezone; # F:XQC_Error ( const Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, int*)* get_implicit_timezone
  464.     has Pointer                       $!set_error_handler; # F:XQC_Error ( Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, Typedef<XQC_ErrorHandler>->|XQC_ErrorHandler_s|*)* set_error_handler
  465.     has Pointer                       $!get_error_handler; # F:XQC_Error ( const Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, Typedef<XQC_ErrorHandler>->|XQC_ErrorHandler_s|**)* get_error_handler
  466.     has Pointer                       $!get_interface; # F:void* ( const Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*, const char*)* get_interface
  467.     has Pointer                       $!free; # F:void ( Typedef<XQC_DynamicContext>->|XQC_DynamicContext_s|*)* free
  468.  
  469.     submethod DESTROY {
  470.         $!free();
  471.     }
  472. }
  473.  
  474. constant XQC_Expression is export := XQC_Expression_s;
  475. constant XQC_Sequence is export := XQC_Sequence_s;
  476. constant XQC_InputStream is export := XQC_InputStream_s;
  477. constant XQC_Implementation is export := XQC_Implementation_s;
  478. constant XQC_ErrorHandler is export := XQC_ErrorHandler_s;
  479. constant XQC_DynamicContext is export := XQC_DynamicContext_s;
  480. constant XQC_StaticContext is export := XQC_StaticContext_s;
  481.  
  482. #
  483. #  Creates an XQC_Implementation object that uses XQilla.
  484. #
  485. # ```C
  486. # XQC_Implementation *createXQillaXQCImplementation(int version);
  487. # ```
  488. sub createXQillaXQCImplementation(int32 $version)
  489.     is native('libxqilla') returns XQC_Implementation_s
  490.     is export { * }
  491.  
  492. sub testErr($err) {
  493.     # Throw exception based on value of my $err.
  494.     return unless $err != 0;
  495.  
  496.     my $msg = do given $err {
  497.         when XQC_END_OF_SEQUENCE {
  498.             "End of Sequence";
  499.         }
  500.    
  501.         when XQC_NO_CURRENT_ITEM {
  502.             "No current item";
  503.         }
  504.  
  505.         when XQC_PARSE_ERROR {
  506.             "Parse error";
  507.         }
  508.  
  509.         when XQC_INVALID_ARGUMENT {
  510.             "Invalid argument";
  511.         }
  512.  
  513.         when XQC_NOT_NODE {
  514.             "Not node";
  515.         }
  516.  
  517.         when XQC_INTERNAL_ERROR {
  518.             "Internal error";
  519.         }
  520.  
  521.         when XQC_NOT_IMPLEMENTED {
  522.             "Not implemented";
  523.         }
  524.  
  525.         when XQC_UNRECOGNIZED_ENCODING {
  526.             "Unrecognized encoding";
  527.         }
  528.  
  529.         when XQC_STATIC_ERROR {
  530.             "Static error";
  531.         }
  532.  
  533.         when XQC_TYPE_ERROR {
  534.             "Type error";
  535.         }
  536.  
  537.         when XQC_DYNAMIC_ERROR {
  538.             "Dynamic error";
  539.         }
  540.  
  541.         when XQC_SERIALIZATION_ERROR {
  542.             "Serialization error";
  543.         }
  544.     }
  545.  
  546.     die "An XQilla error has occurred: $msg";
  547. }
  548.  
  549. # cw: ---YYY--- It would be nice to make the following subs private to this module, but
  550. #     it is enough that they are not exported by default.
  551.  
  552. # cw: ---YYY--- Insure that the use of int32 is correct on 64-bit systems.
  553. # To get FILE* from IO::Handle
  554. sub fdopen(int32, str)
  555.     is native('libc')
  556.     returns Pointer { * };
  557.  
  558. sub fdclose(int32)
  559.     is native('libc')
  560.     { * };
  561.  
  562. sub free(Pointer)
  563.     is native('libc')
  564.     { * };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement