| author | |
| committer | |
| log | e3bf40742d5b93c10b8e68b202294badb508c3e7 |
| tree | 73a9219fd0f5ec7985eb9f121390fa2db24654f9 |
| parent | bb3ac177a83caf52556818e9ea163ac166bb6c07 |
| parent | 1fb15be05f1037aad53d2db32d13123363365d10 |
| signature | Commit is signed but in an unrecognized format. |
21 files changed, 876 insertions(+), 551 deletions(-)
README.md+65-27| ... | @@ -42,33 +42,71 @@ clarity. | ... | @@ -42,33 +42,71 @@ clarity. |
| 42 | * In addition to creating executables, creating a C library is a primary use | 42 | * In addition to creating executables, creating a C library is a primary use |
| 43 | case. You can export an auto-generated .h file. | 43 | case. You can export an auto-generated .h file. |
| 44 | 44 | ||
| 45 | ### Support Table | 45 | ### Supported Targets |
| 46 | 46 | ||
| 47 | Freestanding means that you do not directly interact with the OS | 47 | #### Tier 1 Support |
| 48 | or you are writing your own OS. | 48 | |
| 49 | 49 | * Not only can Zig generate machine code for these targets, but the standard | |
| 50 | Note that if you use libc or other libraries to interact with the OS, | 50 | library cross-platform abstractions have implementations for these targets. |
| 51 | that counts as "freestanding" for the purposes of this table. | 51 | Thus it is practical to write a pure Zig application with no dependency on |
| 52 | 52 | libc. | |
| 53 | | | freestanding | linux | macosx | windows | other | | 53 | * The CI server automatically tests these targets on every commit to master |
| 54 | |-------------|--------------|---------|---------|---------|---------| | 54 | branch, and updates ziglang.org/download with links to pre-built binaries. |
| 55 | |i386 | OK | planned | OK | planned | planned | | 55 | * These targets have debug info capabilities and therefore produce stack |
| 56 | |x86_64 | OK | OK | OK | OK | planned | | 56 | traces on failed assertions. |
| 57 | |arm | OK | planned | planned | planned | planned | | 57 | |
| 58 | |bpf | OK | planned | N/A | N/A | planned | | 58 | #### Tier 2 Support |
| 59 | |hexagon | OK | planned | N/A | N/A | planned | | 59 | |
| 60 | |mips | OK | planned | N/A | N/A | planned | | 60 | * There may be some standard library implementations, but many abstractions |
| 61 | |powerpc | OK | planned | N/A | N/A | planned | | 61 | will give an "Unsupported OS" compile error. One can link with libc or other |
| 62 | |r600 | OK | planned | N/A | N/A | planned | | 62 | libraries to fill in the gaps in the standard library. |
| 63 | |amdgcn | OK | planned | N/A | N/A | planned | | 63 | * These targets are known to work, but are not automatically tested, so there |
| 64 | |sparc | OK | planned | N/A | N/A | planned | | 64 | are occasional regressions. |
| 65 | |s390x | OK | planned | N/A | N/A | planned | | 65 | * Some tests may be disabled for these targets as we work toward Tier 1 |
| 66 | |spir | OK | planned | N/A | N/A | planned | | 66 | support. |
| 67 | |lanai | OK | planned | N/A | N/A | planned | | 67 | |
| 68 | |wasm32 | planned | N/A | N/A | N/A | N/A | | 68 | #### Tier 3 Support |
| 69 | |wasm64 | planned | N/A | N/A | N/A | N/A | | 69 | |
| 70 | |riscv32 | planned | planned | N/A | N/A | planned | | 70 | * The standard library has little to no knowledge of the existence of this |
| 71 | |riscv64 | planned | planned | N/A | N/A | planned | | 71 | target. |
| 72 | * Because Zig is based on LLVM, it has the capability to build for these | ||
| 73 | targets, and LLVM has the target enabled by default. | ||
| 74 | * These targets are not frequently tested; one will likely need to contribute | ||
| 75 | to Zig in order to build for these targets. | ||
| 76 | * The Zig compiler might need to be updated with a few things such as | ||
| 77 | - what sizes are the C integer types | ||
| 78 | - C ABI calling convention for this target | ||
| 79 | - bootstrap code and default panic handler | ||
| 80 | |||
| 81 | #### Tier 4 Support | ||
| 82 | |||
| 83 | * Support for these targets is entirely experimental. | ||
| 84 | * LLVM may have the target as an experimental target, which means that you | ||
| 85 | need to use Zig-provided binaries for the target to be available, or | ||
| 86 | build LLVM from source with special configure flags. | ||
| 87 | |||
| 88 | #### Support Table | ||
| 89 | |||
| 90 | | | freestanding | linux | macosx | windows | freebsd | other | | ||
| 91 | |--------|--------------|--------|--------|---------|---------|--------| | ||
| 92 | |x86_64 | Tier 2 | Tier 1 | Tier 1 | Tier 1 | Tier 3 | Tier 3 | | ||
| 93 | |i386 | Tier 2 | Tier 2 | Tier 2 | Tier 2 | Tier 3 | Tier 3 | | ||
| 94 | |arm | Tier 2 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | | ||
| 95 | |arm64 | Tier 2 | Tier 2 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | | ||
| 96 | |bpf | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 97 | |hexagon | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 98 | |mips | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 99 | |powerpc | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 100 | |r600 | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 101 | |amdgcn | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 102 | |sparc | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 103 | |s390x | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 104 | |spir | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 105 | |lanai | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | | ||
| 106 | |wasm32 | Tier 4 | N/A | N/A | N/A | N/A | N/A | | ||
| 107 | |wasm64 | Tier 4 | N/A | N/A | N/A | N/A | N/A | | ||
| 108 | |riscv32 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | | ||
| 109 | |riscv64 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | | ||
| 72 | 110 | ||
| 73 | ## Community | 111 | ## Community |
| 74 | 112 |
src/analyze.cpp+49-36| ... | @@ -1619,13 +1619,16 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc | ... | @@ -1619,13 +1619,16 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc |
| 1619 | case ZigTypeIdUnion: | 1619 | case ZigTypeIdUnion: |
| 1620 | case ZigTypeIdFn: | 1620 | case ZigTypeIdFn: |
| 1621 | case ZigTypeIdPromise: | 1621 | case ZigTypeIdPromise: |
| 1622 | if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown))) | 1622 | switch (type_requires_comptime(g, type_entry)) { |
| 1623 | return g->builtin_types.entry_invalid; | 1623 | case ReqCompTimeNo: |
| 1624 | if (type_requires_comptime(type_entry)) { | 1624 | break; |
| 1625 | add_node_error(g, param_node->data.param_decl.type, | 1625 | case ReqCompTimeYes: |
| 1626 | buf_sprintf("parameter of type '%s' must be declared comptime", | 1626 | add_node_error(g, param_node->data.param_decl.type, |
| 1627 | buf_ptr(&type_entry->name))); | 1627 | buf_sprintf("parameter of type '%s' must be declared comptime", |
| 1628 | return g->builtin_types.entry_invalid; | 1628 | buf_ptr(&type_entry->name))); |
| 1629 | return g->builtin_types.entry_invalid; | ||
| 1630 | case ReqCompTimeInvalid: | ||
| 1631 | return g->builtin_types.entry_invalid; | ||
| 1629 | } | 1632 | } |
| 1630 | break; | 1633 | break; |
| 1631 | } | 1634 | } |
| ... | @@ -1711,10 +1714,13 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc | ... | @@ -1711,10 +1714,13 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc |
| 1711 | case ZigTypeIdUnion: | 1714 | case ZigTypeIdUnion: |
| 1712 | case ZigTypeIdFn: | 1715 | case ZigTypeIdFn: |
| 1713 | case ZigTypeIdPromise: | 1716 | case ZigTypeIdPromise: |
| 1714 | if ((err = type_resolve(g, fn_type_id.return_type, ResolveStatusZeroBitsKnown))) | 1717 | switch (type_requires_comptime(g, fn_type_id.return_type)) { |
| 1715 | return g->builtin_types.entry_invalid; | 1718 | case ReqCompTimeInvalid: |
| 1716 | if (type_requires_comptime(fn_type_id.return_type)) { | 1719 | return g->builtin_types.entry_invalid; |
| 1717 | return get_generic_fn_type(g, &fn_type_id); | 1720 | case ReqCompTimeYes: |
| 1721 | return get_generic_fn_type(g, &fn_type_id); | ||
| 1722 | case ReqCompTimeNo: | ||
| 1723 | break; | ||
| 1718 | } | 1724 | } |
| 1719 | break; | 1725 | break; |
| 1720 | } | 1726 | } |
| ... | @@ -2560,8 +2566,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { | ... | @@ -2560,8 +2566,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2560 | static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { | 2566 | static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2561 | assert(struct_type->id == ZigTypeIdStruct); | 2567 | assert(struct_type->id == ZigTypeIdStruct); |
| 2562 | 2568 | ||
| 2563 | Error err; | ||
| 2564 | |||
| 2565 | if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) | 2569 | if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) |
| 2566 | return ErrorSemanticAnalyzeFail; | 2570 | return ErrorSemanticAnalyzeFail; |
| 2567 | if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown) | 2571 | if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown) |
| ... | @@ -2619,13 +2623,15 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { | ... | @@ -2619,13 +2623,15 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { |
| 2619 | buf_sprintf("enums, not structs, support field assignment")); | 2623 | buf_sprintf("enums, not structs, support field assignment")); |
| 2620 | } | 2624 | } |
| 2621 | 2625 | ||
| 2622 | if ((err = type_resolve(g, field_type, ResolveStatusZeroBitsKnown))) { | 2626 | switch (type_requires_comptime(g, field_type)) { |
| 2623 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; | 2627 | case ReqCompTimeYes: |
| 2624 | continue; | 2628 | struct_type->data.structure.requires_comptime = true; |
| 2625 | } | 2629 | break; |
| 2626 | 2630 | case ReqCompTimeInvalid: | |
| 2627 | if (type_requires_comptime(field_type)) { | 2631 | struct_type->data.structure.resolve_status = ResolveStatusInvalid; |
| 2628 | struct_type->data.structure.requires_comptime = true; | 2632 | continue; |
| 2633 | case ReqCompTimeNo: | ||
| 2634 | break; | ||
| 2629 | } | 2635 | } |
| 2630 | 2636 | ||
| 2631 | if (!type_has_bits(field_type)) | 2637 | if (!type_has_bits(field_type)) |
| ... | @@ -2890,11 +2896,17 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { | ... | @@ -2890,11 +2896,17 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2890 | } | 2896 | } |
| 2891 | union_field->type_entry = field_type; | 2897 | union_field->type_entry = field_type; |
| 2892 | 2898 | ||
| 2893 | if (type_requires_comptime(field_type)) { | 2899 | switch (type_requires_comptime(g, field_type)) { |
| 2894 | union_type->data.unionation.requires_comptime = true; | 2900 | case ReqCompTimeInvalid: |
| 2901 | union_type->data.unionation.is_invalid = true; | ||
| 2902 | continue; | ||
| 2903 | case ReqCompTimeYes: | ||
| 2904 | union_type->data.unionation.requires_comptime = true; | ||
| 2905 | break; | ||
| 2906 | case ReqCompTimeNo: | ||
| 2907 | break; | ||
| 2895 | } | 2908 | } |
| 2896 | 2909 | ||
| 2897 | |||
| 2898 | if (field_node->data.struct_field.value != nullptr && !decl_node->data.container_decl.auto_enum) { | 2910 | if (field_node->data.struct_field.value != nullptr && !decl_node->data.container_decl.auto_enum) { |
| 2899 | ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.value, | 2911 | ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.value, |
| 2900 | buf_sprintf("non-enum union field assignment")); | 2912 | buf_sprintf("non-enum union field assignment")); |
| ... | @@ -5096,7 +5108,10 @@ bool type_has_bits(ZigType *type_entry) { | ... | @@ -5096,7 +5108,10 @@ bool type_has_bits(ZigType *type_entry) { |
| 5096 | return !type_entry->zero_bits; | 5108 | return !type_entry->zero_bits; |
| 5097 | } | 5109 | } |
| 5098 | 5110 | ||
| 5099 | bool type_requires_comptime(ZigType *type_entry) { | 5111 | ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) { |
| 5112 | Error err; | ||
| 5113 | if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown))) | ||
| 5114 | return ReqCompTimeInvalid; | ||
| 5100 | switch (type_entry->id) { | 5115 | switch (type_entry->id) { |
| 5101 | case ZigTypeIdInvalid: | 5116 | case ZigTypeIdInvalid: |
| 5102 | case ZigTypeIdOpaque: | 5117 | case ZigTypeIdOpaque: |
| ... | @@ -5109,27 +5124,25 @@ bool type_requires_comptime(ZigType *type_entry) { | ... | @@ -5109,27 +5124,25 @@ bool type_requires_comptime(ZigType *type_entry) { |
| 5109 | case ZigTypeIdNamespace: | 5124 | case ZigTypeIdNamespace: |
| 5110 | case ZigTypeIdBoundFn: | 5125 | case ZigTypeIdBoundFn: |
| 5111 | case ZigTypeIdArgTuple: | 5126 | case ZigTypeIdArgTuple: |
| 5112 | return true; | 5127 | return ReqCompTimeYes; |
| 5113 | case ZigTypeIdArray: | 5128 | case ZigTypeIdArray: |
| 5114 | return type_requires_comptime(type_entry->data.array.child_type); | 5129 | return type_requires_comptime(g, type_entry->data.array.child_type); |
| 5115 | case ZigTypeIdStruct: | 5130 | case ZigTypeIdStruct: |
| 5116 | assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown)); | 5131 | return type_entry->data.structure.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo; |
| 5117 | return type_entry->data.structure.requires_comptime; | ||
| 5118 | case ZigTypeIdUnion: | 5132 | case ZigTypeIdUnion: |
| 5119 | assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown)); | 5133 | return type_entry->data.unionation.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo; |
| 5120 | return type_entry->data.unionation.requires_comptime; | ||
| 5121 | case ZigTypeIdOptional: | 5134 | case ZigTypeIdOptional: |
| 5122 | return type_requires_comptime(type_entry->data.maybe.child_type); | 5135 | return type_requires_comptime(g, type_entry->data.maybe.child_type); |
| 5123 | case ZigTypeIdErrorUnion: | 5136 | case ZigTypeIdErrorUnion: |
| 5124 | return type_requires_comptime(type_entry->data.error_union.payload_type); | 5137 | return type_requires_comptime(g, type_entry->data.error_union.payload_type); |
| 5125 | case ZigTypeIdPointer: | 5138 | case ZigTypeIdPointer: |
| 5126 | if (type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) { | 5139 | if (type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) { |
| 5127 | return false; | 5140 | return ReqCompTimeNo; |
| 5128 | } else { | 5141 | } else { |
| 5129 | return type_requires_comptime(type_entry->data.pointer.child_type); | 5142 | return type_requires_comptime(g, type_entry->data.pointer.child_type); |
| 5130 | } | 5143 | } |
| 5131 | case ZigTypeIdFn: | 5144 | case ZigTypeIdFn: |
| 5132 | return type_entry->data.fn.is_generic; | 5145 | return type_entry->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo; |
| 5133 | case ZigTypeIdEnum: | 5146 | case ZigTypeIdEnum: |
| 5134 | case ZigTypeIdErrorSet: | 5147 | case ZigTypeIdErrorSet: |
| 5135 | case ZigTypeIdBool: | 5148 | case ZigTypeIdBool: |
| ... | @@ -5138,7 +5151,7 @@ bool type_requires_comptime(ZigType *type_entry) { | ... | @@ -5138,7 +5151,7 @@ bool type_requires_comptime(ZigType *type_entry) { |
| 5138 | case ZigTypeIdVoid: | 5151 | case ZigTypeIdVoid: |
| 5139 | case ZigTypeIdUnreachable: | 5152 | case ZigTypeIdUnreachable: |
| 5140 | case ZigTypeIdPromise: | 5153 | case ZigTypeIdPromise: |
| 5141 | return false; | 5154 | return ReqCompTimeNo; |
| 5142 | } | 5155 | } |
| 5143 | zig_unreachable(); | 5156 | zig_unreachable(); |
| 5144 | } | 5157 | } |
src/analyze.hpp+7-1| ... | @@ -87,7 +87,6 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node); | ... | @@ -87,7 +87,6 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node); |
| 87 | ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value); | 87 | ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value); |
| 88 | void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc); | 88 | void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc); |
| 89 | AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index); | 89 | AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index); |
| 90 | bool type_requires_comptime(ZigType *type_entry); | ||
| 91 | Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry); | 90 | Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry); |
| 92 | Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status); | 91 | Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status); |
| 93 | void complete_enum(CodeGen *g, ZigType *enum_type); | 92 | void complete_enum(CodeGen *g, ZigType *enum_type); |
| ... | @@ -216,4 +215,11 @@ bool want_first_arg_sret(CodeGen *g, FnTypeId *fn_type_id); | ... | @@ -216,4 +215,11 @@ bool want_first_arg_sret(CodeGen *g, FnTypeId *fn_type_id); |
| 216 | 215 | ||
| 217 | uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field); | 216 | uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field); |
| 218 | 217 | ||
| 218 | enum ReqCompTime { | ||
| 219 | ReqCompTimeInvalid, | ||
| 220 | ReqCompTimeNo, | ||
| 221 | ReqCompTimeYes, | ||
| 222 | }; | ||
| 223 | ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry); | ||
| 224 | |||
| 219 | #endif | 225 | #endif |
src/cache_hash.cpp+4-2| ... | @@ -352,8 +352,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { | ... | @@ -352,8 +352,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { |
| 352 | // if the mtime matches we can trust the digest | 352 | // if the mtime matches we can trust the digest |
| 353 | OsFile this_file; | 353 | OsFile this_file; |
| 354 | if ((err = os_file_open_r(chf->path, &this_file))) { | 354 | if ((err = os_file_open_r(chf->path, &this_file))) { |
| 355 | fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err)); | ||
| 355 | os_file_close(ch->manifest_file); | 356 | os_file_close(ch->manifest_file); |
| 356 | return err; | 357 | return ErrorCacheUnavailable; |
| 357 | } | 358 | } |
| 358 | OsTimeStamp actual_mtime; | 359 | OsTimeStamp actual_mtime; |
| 359 | if ((err = os_file_mtime(this_file, &actual_mtime))) { | 360 | if ((err = os_file_mtime(this_file, &actual_mtime))) { |
| ... | @@ -392,8 +393,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { | ... | @@ -392,8 +393,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { |
| 392 | for (; file_i < input_file_count; file_i += 1) { | 393 | for (; file_i < input_file_count; file_i += 1) { |
| 393 | CacheHashFile *chf = &ch->files.at(file_i); | 394 | CacheHashFile *chf = &ch->files.at(file_i); |
| 394 | if ((err = populate_file_hash(ch, chf, nullptr))) { | 395 | if ((err = populate_file_hash(ch, chf, nullptr))) { |
| 396 | fprintf(stderr, "Unable to hash %s: %s\n", buf_ptr(chf->path), err_str(err)); | ||
| 395 | os_file_close(ch->manifest_file); | 397 | os_file_close(ch->manifest_file); |
| 396 | return err; | 398 | return ErrorCacheUnavailable; |
| 397 | } | 399 | } |
| 398 | } | 400 | } |
| 399 | return ErrorNone; | 401 | return ErrorNone; |
src/codegen.cpp+19-4| ... | @@ -129,6 +129,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out | ... | @@ -129,6 +129,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out |
| 129 | Buf *src_dir = buf_alloc(); | 129 | Buf *src_dir = buf_alloc(); |
| 130 | os_path_split(root_src_path, src_dir, src_basename); | 130 | os_path_split(root_src_path, src_dir, src_basename); |
| 131 | 131 | ||
| 132 | if (buf_len(src_basename) == 0) { | ||
| 133 | fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path)); | ||
| 134 | exit(1); | ||
| 135 | } | ||
| 136 | |||
| 132 | g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename)); | 137 | g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename)); |
| 133 | g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig"); | 138 | g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig"); |
| 134 | g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); | 139 | g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); |
| ... | @@ -1645,7 +1650,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z | ... | @@ -1645,7 +1650,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z |
| 1645 | zig_unreachable(); | 1650 | zig_unreachable(); |
| 1646 | } | 1651 | } |
| 1647 | 1652 | ||
| 1648 | if (actual_bits >= wanted_bits && actual_type->id == ZigTypeIdInt && | 1653 | if (actual_type->id == ZigTypeIdInt && |
| 1649 | !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed && | 1654 | !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed && |
| 1650 | want_runtime_safety) | 1655 | want_runtime_safety) |
| 1651 | { | 1656 | { |
| ... | @@ -6281,8 +6286,14 @@ static void do_code_gen(CodeGen *g) { | ... | @@ -6281,8 +6286,14 @@ static void do_code_gen(CodeGen *g) { |
| 6281 | } | 6286 | } |
| 6282 | if (ir_get_var_is_comptime(var)) | 6287 | if (ir_get_var_is_comptime(var)) |
| 6283 | continue; | 6288 | continue; |
| 6284 | if (type_requires_comptime(var->value->type)) | 6289 | switch (type_requires_comptime(g, var->value->type)) { |
| 6285 | continue; | 6290 | case ReqCompTimeInvalid: |
| 6291 | zig_unreachable(); | ||
| 6292 | case ReqCompTimeYes: | ||
| 6293 | continue; | ||
| 6294 | case ReqCompTimeNo: | ||
| 6295 | break; | ||
| 6296 | } | ||
| 6286 | 6297 | ||
| 6287 | if (var->src_arg_index == SIZE_MAX) { | 6298 | if (var->src_arg_index == SIZE_MAX) { |
| 6288 | var->value_ref = build_alloca(g, var->value->type, buf_ptr(&var->name), var->align_bytes); | 6299 | var->value_ref = build_alloca(g, var->value->type, buf_ptr(&var->name), var->align_bytes); |
| ... | @@ -8172,7 +8183,11 @@ void codegen_build_and_link(CodeGen *g) { | ... | @@ -8172,7 +8183,11 @@ void codegen_build_and_link(CodeGen *g) { |
| 8172 | os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir); | 8183 | os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir); |
| 8173 | 8184 | ||
| 8174 | if ((err = check_cache(g, manifest_dir, &digest))) { | 8185 | if ((err = check_cache(g, manifest_dir, &digest))) { |
| 8175 | fprintf(stderr, "Unable to check cache: %s\n", err_str(err)); | 8186 | if (err == ErrorCacheUnavailable) { |
| 8187 | // message already printed | ||
| 8188 | } else { | ||
| 8189 | fprintf(stderr, "Unable to check cache: %s\n", err_str(err)); | ||
| 8190 | } | ||
| 8176 | exit(1); | 8191 | exit(1); |
| 8177 | } | 8192 | } |
| 8178 | 8193 |
src/error.cpp+1| ... | @@ -33,6 +33,7 @@ const char *err_str(Error err) { | ... | @@ -33,6 +33,7 @@ const char *err_str(Error err) { |
| 33 | case ErrorSharingViolation: return "sharing violation"; | 33 | case ErrorSharingViolation: return "sharing violation"; |
| 34 | case ErrorPipeBusy: return "pipe busy"; | 34 | case ErrorPipeBusy: return "pipe busy"; |
| 35 | case ErrorPrimitiveTypeNotFound: return "primitive type not found"; | 35 | case ErrorPrimitiveTypeNotFound: return "primitive type not found"; |
| 36 | case ErrorCacheUnavailable: return "cache unavailable"; | ||
| 36 | } | 37 | } |
| 37 | return "(invalid error)"; | 38 | return "(invalid error)"; |
| 38 | } | 39 | } |
src/error.hpp+1| ... | @@ -35,6 +35,7 @@ enum Error { | ... | @@ -35,6 +35,7 @@ enum Error { |
| 35 | ErrorSharingViolation, | 35 | ErrorSharingViolation, |
| 36 | ErrorPipeBusy, | 36 | ErrorPipeBusy, |
| 37 | ErrorPrimitiveTypeNotFound, | 37 | ErrorPrimitiveTypeNotFound, |
| 38 | ErrorCacheUnavailable, | ||
| 38 | }; | 39 | }; |
| 39 | 40 | ||
| 40 | const char *err_str(Error err); | 41 | const char *err_str(Error err); |
src/ir.cpp+65-45| ... | @@ -11276,7 +11276,6 @@ static bool optional_value_is_null(ConstExprValue *val) { | ... | @@ -11276,7 +11276,6 @@ static bool optional_value_is_null(ConstExprValue *val) { |
| 11276 | } | 11276 | } |
| 11277 | 11277 | ||
| 11278 | static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) { | 11278 | static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) { |
| 11279 | Error err; | ||
| 11280 | IrInstruction *op1 = bin_op_instruction->op1->child; | 11279 | IrInstruction *op1 = bin_op_instruction->op1->child; |
| 11281 | if (type_is_invalid(op1->value.type)) | 11280 | if (type_is_invalid(op1->value.type)) |
| 11282 | return ira->codegen->invalid_instruction; | 11281 | return ira->codegen->invalid_instruction; |
| ... | @@ -11470,10 +11469,19 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp * | ... | @@ -11470,10 +11469,19 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp * |
| 11470 | if (casted_op2 == ira->codegen->invalid_instruction) | 11469 | if (casted_op2 == ira->codegen->invalid_instruction) |
| 11471 | return ira->codegen->invalid_instruction; | 11470 | return ira->codegen->invalid_instruction; |
| 11472 | 11471 | ||
| 11473 | if ((err = type_resolve(ira->codegen, resolved_type, ResolveStatusZeroBitsKnown))) | 11472 | bool requires_comptime; |
| 11474 | return ira->codegen->invalid_instruction; | 11473 | switch (type_requires_comptime(ira->codegen, resolved_type)) { |
| 11474 | case ReqCompTimeYes: | ||
| 11475 | requires_comptime = true; | ||
| 11476 | break; | ||
| 11477 | case ReqCompTimeNo: | ||
| 11478 | requires_comptime = false; | ||
| 11479 | break; | ||
| 11480 | case ReqCompTimeInvalid: | ||
| 11481 | return ira->codegen->invalid_instruction; | ||
| 11482 | } | ||
| 11475 | 11483 | ||
| 11476 | bool one_possible_value = !type_requires_comptime(resolved_type) && !type_has_bits(resolved_type); | 11484 | bool one_possible_value = !requires_comptime && !type_has_bits(resolved_type); |
| 11477 | if (one_possible_value || (instr_is_comptime(casted_op1) && instr_is_comptime(casted_op2))) { | 11485 | if (one_possible_value || (instr_is_comptime(casted_op1) && instr_is_comptime(casted_op2))) { |
| 11478 | ConstExprValue *op1_val = one_possible_value ? &casted_op1->value : ir_resolve_const(ira, casted_op1, UndefBad); | 11486 | ConstExprValue *op1_val = one_possible_value ? &casted_op1->value : ir_resolve_const(ira, casted_op1, UndefBad); |
| 11479 | if (op1_val == nullptr) | 11487 | if (op1_val == nullptr) |
| ... | @@ -12406,42 +12414,41 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruct | ... | @@ -12406,42 +12414,41 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruct |
| 12406 | ZigType *result_type = casted_init_value->value.type; | 12414 | ZigType *result_type = casted_init_value->value.type; |
| 12407 | if (type_is_invalid(result_type)) { | 12415 | if (type_is_invalid(result_type)) { |
| 12408 | result_type = ira->codegen->builtin_types.entry_invalid; | 12416 | result_type = ira->codegen->builtin_types.entry_invalid; |
| 12409 | } else { | 12417 | } else if (result_type->id == ZigTypeIdUnreachable || result_type->id == ZigTypeIdOpaque) { |
| 12410 | if ((err = type_resolve(ira->codegen, result_type, ResolveStatusZeroBitsKnown))) { | 12418 | ir_add_error_node(ira, source_node, |
| 12411 | result_type = ira->codegen->builtin_types.entry_invalid; | 12419 | buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name))); |
| 12412 | } | 12420 | result_type = ira->codegen->builtin_types.entry_invalid; |
| 12413 | } | 12421 | } |
| 12414 | 12422 | ||
| 12415 | if (!type_is_invalid(result_type)) { | 12423 | switch (type_requires_comptime(ira->codegen, result_type)) { |
| 12416 | if (result_type->id == ZigTypeIdUnreachable || | 12424 | case ReqCompTimeInvalid: |
| 12417 | result_type->id == ZigTypeIdOpaque) | 12425 | result_type = ira->codegen->builtin_types.entry_invalid; |
| 12418 | { | 12426 | break; |
| 12427 | case ReqCompTimeYes: { | ||
| 12428 | var_class_requires_const = true; | ||
| 12429 | if (!var->gen_is_const && !is_comptime_var) { | ||
| 12419 | ir_add_error_node(ira, source_node, | 12430 | ir_add_error_node(ira, source_node, |
| 12420 | buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name))); | 12431 | buf_sprintf("variable of type '%s' must be const or comptime", |
| 12432 | buf_ptr(&result_type->name))); | ||
| 12421 | result_type = ira->codegen->builtin_types.entry_invalid; | 12433 | result_type = ira->codegen->builtin_types.entry_invalid; |
| 12422 | } else if (type_requires_comptime(result_type)) { | 12434 | } |
| 12435 | break; | ||
| 12436 | } | ||
| 12437 | case ReqCompTimeNo: | ||
| 12438 | if (casted_init_value->value.special == ConstValSpecialStatic && | ||
| 12439 | casted_init_value->value.type->id == ZigTypeIdFn && | ||
| 12440 | casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways) | ||
| 12441 | { | ||
| 12423 | var_class_requires_const = true; | 12442 | var_class_requires_const = true; |
| 12424 | if (!var->gen_is_const && !is_comptime_var) { | 12443 | if (!var->src_is_const && !is_comptime_var) { |
| 12425 | ir_add_error_node(ira, source_node, | 12444 | ErrorMsg *msg = ir_add_error_node(ira, source_node, |
| 12426 | buf_sprintf("variable of type '%s' must be const or comptime", | 12445 | buf_sprintf("functions marked inline must be stored in const or comptime var")); |
| 12427 | buf_ptr(&result_type->name))); | 12446 | AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node; |
| 12447 | add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here")); | ||
| 12428 | result_type = ira->codegen->builtin_types.entry_invalid; | 12448 | result_type = ira->codegen->builtin_types.entry_invalid; |
| 12429 | } | 12449 | } |
| 12430 | } else { | ||
| 12431 | if (casted_init_value->value.special == ConstValSpecialStatic && | ||
| 12432 | casted_init_value->value.type->id == ZigTypeIdFn && | ||
| 12433 | casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways) | ||
| 12434 | { | ||
| 12435 | var_class_requires_const = true; | ||
| 12436 | if (!var->src_is_const && !is_comptime_var) { | ||
| 12437 | ErrorMsg *msg = ir_add_error_node(ira, source_node, | ||
| 12438 | buf_sprintf("functions marked inline must be stored in const or comptime var")); | ||
| 12439 | AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node; | ||
| 12440 | add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here")); | ||
| 12441 | result_type = ira->codegen->builtin_types.entry_invalid; | ||
| 12442 | } | ||
| 12443 | } | ||
| 12444 | } | 12450 | } |
| 12451 | break; | ||
| 12445 | } | 12452 | } |
| 12446 | 12453 | ||
| 12447 | if (var->value->type != nullptr && !is_comptime_var) { | 12454 | if (var->value->type != nullptr && !is_comptime_var) { |
| ... | @@ -12912,10 +12919,15 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod | ... | @@ -12912,10 +12919,15 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod |
| 12912 | } | 12919 | } |
| 12913 | 12920 | ||
| 12914 | if (!comptime_arg) { | 12921 | if (!comptime_arg) { |
| 12915 | if (type_requires_comptime(casted_arg->value.type)) { | 12922 | switch (type_requires_comptime(ira->codegen, casted_arg->value.type)) { |
| 12923 | case ReqCompTimeYes: | ||
| 12916 | ir_add_error(ira, casted_arg, | 12924 | ir_add_error(ira, casted_arg, |
| 12917 | buf_sprintf("parameter of type '%s' requires comptime", buf_ptr(&casted_arg->value.type->name))); | 12925 | buf_sprintf("parameter of type '%s' requires comptime", buf_ptr(&casted_arg->value.type->name))); |
| 12918 | return false; | 12926 | return false; |
| 12927 | case ReqCompTimeInvalid: | ||
| 12928 | return false; | ||
| 12929 | case ReqCompTimeNo: | ||
| 12930 | break; | ||
| 12919 | } | 12931 | } |
| 12920 | 12932 | ||
| 12921 | casted_args[fn_type_id->param_count] = casted_arg; | 12933 | casted_args[fn_type_id->param_count] = casted_arg; |
| ... | @@ -13388,12 +13400,15 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call | ... | @@ -13388,12 +13400,15 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call |
| 13388 | inst_fn_type_id.return_type = specified_return_type; | 13400 | inst_fn_type_id.return_type = specified_return_type; |
| 13389 | } | 13401 | } |
| 13390 | 13402 | ||
| 13391 | if ((err = type_resolve(ira->codegen, specified_return_type, ResolveStatusZeroBitsKnown))) | 13403 | switch (type_requires_comptime(ira->codegen, specified_return_type)) { |
| 13392 | return ira->codegen->invalid_instruction; | 13404 | case ReqCompTimeYes: |
| 13393 | |||
| 13394 | if (type_requires_comptime(specified_return_type)) { | ||
| 13395 | // Throw out our work and call the function as if it were comptime. | 13405 | // Throw out our work and call the function as if it were comptime. |
| 13396 | return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr, true, FnInlineAuto); | 13406 | return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr, |
| 13407 | true, FnInlineAuto); | ||
| 13408 | case ReqCompTimeInvalid: | ||
| 13409 | return ira->codegen->invalid_instruction; | ||
| 13410 | case ReqCompTimeNo: | ||
| 13411 | break; | ||
| 13397 | } | 13412 | } |
| 13398 | } | 13413 | } |
| 13399 | IrInstruction *async_allocator_inst = nullptr; | 13414 | IrInstruction *async_allocator_inst = nullptr; |
| ... | @@ -14334,11 +14349,16 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct | ... | @@ -14334,11 +14349,16 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct |
| 14334 | 14349 | ||
| 14335 | } else { | 14350 | } else { |
| 14336 | // runtime known element index | 14351 | // runtime known element index |
| 14337 | if (type_requires_comptime(return_type)) { | 14352 | switch (type_requires_comptime(ira->codegen, return_type)) { |
| 14353 | case ReqCompTimeYes: | ||
| 14338 | ir_add_error(ira, elem_index, | 14354 | ir_add_error(ira, elem_index, |
| 14339 | buf_sprintf("values of type '%s' must be comptime known, but index value is runtime known", | 14355 | buf_sprintf("values of type '%s' must be comptime known, but index value is runtime known", |
| 14340 | buf_ptr(&return_type->data.pointer.child_type->name))); | 14356 | buf_ptr(&return_type->data.pointer.child_type->name))); |
| 14341 | return ira->codegen->invalid_instruction; | 14357 | return ira->codegen->invalid_instruction; |
| 14358 | case ReqCompTimeInvalid: | ||
| 14359 | return ira->codegen->invalid_instruction; | ||
| 14360 | case ReqCompTimeNo: | ||
| 14361 | break; | ||
| 14342 | } | 14362 | } |
| 14343 | if (ptr_align < abi_align) { | 14363 | if (ptr_align < abi_align) { |
| 14344 | if (elem_size >= ptr_align && elem_size % ptr_align == 0) { | 14364 | if (elem_size >= ptr_align && elem_size % ptr_align == 0) { |
| ... | @@ -19390,7 +19410,6 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira, | ... | @@ -19390,7 +19410,6 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira, |
| 19390 | } | 19410 | } |
| 19391 | 19411 | ||
| 19392 | static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnProto *instruction) { | 19412 | static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnProto *instruction) { |
| 19393 | Error err; | ||
| 19394 | AstNode *proto_node = instruction->base.source_node; | 19413 | AstNode *proto_node = instruction->base.source_node; |
| 19395 | assert(proto_node->type == NodeTypeFnProto); | 19414 | assert(proto_node->type == NodeTypeFnProto); |
| 19396 | 19415 | ||
| ... | @@ -19429,11 +19448,8 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct | ... | @@ -19429,11 +19448,8 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct |
| 19429 | if (type_is_invalid(param_type_value->value.type)) | 19448 | if (type_is_invalid(param_type_value->value.type)) |
| 19430 | return ira->codegen->invalid_instruction; | 19449 | return ira->codegen->invalid_instruction; |
| 19431 | ZigType *param_type = ir_resolve_type(ira, param_type_value); | 19450 | ZigType *param_type = ir_resolve_type(ira, param_type_value); |
| 19432 | if (type_is_invalid(param_type)) | 19451 | switch (type_requires_comptime(ira->codegen, param_type)) { |
| 19433 | return ira->codegen->invalid_instruction; | 19452 | case ReqCompTimeYes: |
| 19434 | if ((err = type_resolve(ira->codegen, param_type, ResolveStatusZeroBitsKnown))) | ||
| 19435 | return ira->codegen->invalid_instruction; | ||
| 19436 | if (type_requires_comptime(param_type)) { | ||
| 19437 | if (!calling_convention_allows_zig_types(fn_type_id.cc)) { | 19453 | if (!calling_convention_allows_zig_types(fn_type_id.cc)) { |
| 19438 | ir_add_error(ira, param_type_value, | 19454 | ir_add_error(ira, param_type_value, |
| 19439 | buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'", | 19455 | buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'", |
| ... | @@ -19443,6 +19459,10 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct | ... | @@ -19443,6 +19459,10 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct |
| 19443 | param_info->type = param_type; | 19459 | param_info->type = param_type; |
| 19444 | fn_type_id.next_param_index += 1; | 19460 | fn_type_id.next_param_index += 1; |
| 19445 | return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id)); | 19461 | return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id)); |
| 19462 | case ReqCompTimeInvalid: | ||
| 19463 | return ira->codegen->invalid_instruction; | ||
| 19464 | case ReqCompTimeNo: | ||
| 19465 | break; | ||
| 19446 | } | 19466 | } |
| 19447 | if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) { | 19467 | if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) { |
| 19448 | ir_add_error(ira, param_type_value, | 19468 | ir_add_error(ira, param_type_value, |
src/os.cpp+9-3| ... | @@ -193,14 +193,20 @@ void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { | ... | @@ -193,14 +193,20 @@ void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { |
| 193 | size_t len = buf_len(full_path); | 193 | size_t len = buf_len(full_path); |
| 194 | if (len != 0) { | 194 | if (len != 0) { |
| 195 | size_t last_index = len - 1; | 195 | size_t last_index = len - 1; |
| 196 | if (os_is_sep(buf_ptr(full_path)[last_index])) { | 196 | char last_char = buf_ptr(full_path)[last_index]; |
| 197 | if (os_is_sep(last_char)) { | ||
| 198 | if (last_index == 0) { | ||
| 199 | if (out_dirname) buf_init_from_mem(out_dirname, &last_char, 1); | ||
| 200 | if (out_basename) buf_init_from_str(out_basename, ""); | ||
| 201 | return; | ||
| 202 | } | ||
| 197 | last_index -= 1; | 203 | last_index -= 1; |
| 198 | } | 204 | } |
| 199 | for (size_t i = last_index;;) { | 205 | for (size_t i = last_index;;) { |
| 200 | uint8_t c = buf_ptr(full_path)[i]; | 206 | uint8_t c = buf_ptr(full_path)[i]; |
| 201 | if (os_is_sep(c)) { | 207 | if (os_is_sep(c)) { |
| 202 | if (out_dirname) { | 208 | if (out_dirname) { |
| 203 | buf_init_from_mem(out_dirname, buf_ptr(full_path), i); | 209 | buf_init_from_mem(out_dirname, buf_ptr(full_path), (i == 0) ? 1 : i); |
| 204 | } | 210 | } |
| 205 | if (out_basename) { | 211 | if (out_basename) { |
| 206 | buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1)); | 212 | buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1)); |
| ... | @@ -1990,7 +1996,7 @@ Error os_file_read(OsFile file, void *ptr, size_t *len) { | ... | @@ -1990,7 +1996,7 @@ Error os_file_read(OsFile file, void *ptr, size_t *len) { |
| 1990 | case EFAULT: | 1996 | case EFAULT: |
| 1991 | zig_unreachable(); | 1997 | zig_unreachable(); |
| 1992 | case EISDIR: | 1998 | case EISDIR: |
| 1993 | zig_unreachable(); | 1999 | return ErrorIsDir; |
| 1994 | default: | 2000 | default: |
| 1995 | return ErrorFileSystem; | 2001 | return ErrorFileSystem; |
| 1996 | } | 2002 | } |
std/debug/index.zig+3-2| ... | @@ -282,8 +282,9 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres | ... | @@ -282,8 +282,9 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres |
| 282 | 282 | ||
| 283 | var coff_section: *coff.Section = undefined; | 283 | var coff_section: *coff.Section = undefined; |
| 284 | const mod_index = for (di.sect_contribs) |sect_contrib| { | 284 | const mod_index = for (di.sect_contribs) |sect_contrib| { |
| 285 | if (sect_contrib.Section >= di.coff.sections.len) continue; | 285 | if (sect_contrib.Section > di.coff.sections.len) continue; |
| 286 | coff_section = &di.coff.sections.toSlice()[sect_contrib.Section]; | 286 | // Remember that SectionContribEntry.Section is 1-based. |
| 287 | coff_section = &di.coff.sections.toSlice()[sect_contrib.Section-1]; | ||
| 287 | 288 | ||
| 288 | const vaddr_start = coff_section.header.virtual_address + sect_contrib.Offset; | 289 | const vaddr_start = coff_section.header.virtual_address + sect_contrib.Offset; |
| 289 | const vaddr_end = vaddr_start + sect_contrib.Size; | 290 | const vaddr_end = vaddr_start + sect_contrib.Size; |
std/json.zig+17-10| ... | @@ -910,7 +910,7 @@ fn checkNext(p: *TokenStream, id: Token.Id) void { | ... | @@ -910,7 +910,7 @@ fn checkNext(p: *TokenStream, id: Token.Id) void { |
| 910 | debug.assert(token.id == id); | 910 | debug.assert(token.id == id); |
| 911 | } | 911 | } |
| 912 | 912 | ||
| 913 | test "token" { | 913 | test "json.token" { |
| 914 | const s = | 914 | const s = |
| 915 | \\{ | 915 | \\{ |
| 916 | \\ "Image": { | 916 | \\ "Image": { |
| ... | @@ -980,7 +980,7 @@ pub fn validate(s: []const u8) bool { | ... | @@ -980,7 +980,7 @@ pub fn validate(s: []const u8) bool { |
| 980 | return p.complete; | 980 | return p.complete; |
| 981 | } | 981 | } |
| 982 | 982 | ||
| 983 | test "json validate" { | 983 | test "json.validate" { |
| 984 | debug.assert(validate("{}")); | 984 | debug.assert(validate("{}")); |
| 985 | } | 985 | } |
| 986 | 986 | ||
| ... | @@ -1188,7 +1188,7 @@ pub const Parser = struct { | ... | @@ -1188,7 +1188,7 @@ pub const Parser = struct { |
| 1188 | } | 1188 | } |
| 1189 | 1189 | ||
| 1190 | var value = p.stack.pop(); | 1190 | var value = p.stack.pop(); |
| 1191 | try p.pushToParent(value); | 1191 | try p.pushToParent(&value); |
| 1192 | }, | 1192 | }, |
| 1193 | Token.Id.String => { | 1193 | Token.Id.String => { |
| 1194 | try p.stack.append(try p.parseString(allocator, token, input, i)); | 1194 | try p.stack.append(try p.parseString(allocator, token, input, i)); |
| ... | @@ -1251,7 +1251,7 @@ pub const Parser = struct { | ... | @@ -1251,7 +1251,7 @@ pub const Parser = struct { |
| 1251 | } | 1251 | } |
| 1252 | 1252 | ||
| 1253 | var value = p.stack.pop(); | 1253 | var value = p.stack.pop(); |
| 1254 | try p.pushToParent(value); | 1254 | try p.pushToParent(&value); |
| 1255 | }, | 1255 | }, |
| 1256 | Token.Id.ObjectBegin => { | 1256 | Token.Id.ObjectBegin => { |
| 1257 | try p.stack.append(Value{ .Object = ObjectMap.init(allocator) }); | 1257 | try p.stack.append(Value{ .Object = ObjectMap.init(allocator) }); |
| ... | @@ -1312,19 +1312,19 @@ pub const Parser = struct { | ... | @@ -1312,19 +1312,19 @@ pub const Parser = struct { |
| 1312 | } | 1312 | } |
| 1313 | } | 1313 | } |
| 1314 | 1314 | ||
| 1315 | fn pushToParent(p: *Parser, value: Value) !void { | 1315 | fn pushToParent(p: *Parser, value: *const Value) !void { |
| 1316 | switch (p.stack.at(p.stack.len - 1)) { | 1316 | switch (p.stack.toSlice()[p.stack.len - 1]) { |
| 1317 | // Object Parent -> [ ..., object, <key>, value ] | 1317 | // Object Parent -> [ ..., object, <key>, value ] |
| 1318 | Value.String => |key| { | 1318 | Value.String => |key| { |
| 1319 | _ = p.stack.pop(); | 1319 | _ = p.stack.pop(); |
| 1320 | 1320 | ||
| 1321 | var object = &p.stack.items[p.stack.len - 1].Object; | 1321 | var object = &p.stack.items[p.stack.len - 1].Object; |
| 1322 | _ = try object.put(key, value); | 1322 | _ = try object.put(key, value.*); |
| 1323 | p.state = State.ObjectKey; | 1323 | p.state = State.ObjectKey; |
| 1324 | }, | 1324 | }, |
| 1325 | // Array Parent -> [ ..., <array>, value ] | 1325 | // Array Parent -> [ ..., <array>, value ] |
| 1326 | Value.Array => |*array| { | 1326 | Value.Array => |*array| { |
| 1327 | try array.append(value); | 1327 | try array.append(value.*); |
| 1328 | p.state = State.ArrayValue; | 1328 | p.state = State.ArrayValue; |
| 1329 | }, | 1329 | }, |
| 1330 | else => { | 1330 | else => { |
| ... | @@ -1348,7 +1348,7 @@ pub const Parser = struct { | ... | @@ -1348,7 +1348,7 @@ pub const Parser = struct { |
| 1348 | } | 1348 | } |
| 1349 | }; | 1349 | }; |
| 1350 | 1350 | ||
| 1351 | test "json parser dynamic" { | 1351 | test "json.parser.dynamic" { |
| 1352 | var p = Parser.init(debug.global_allocator, false); | 1352 | var p = Parser.init(debug.global_allocator, false); |
| 1353 | defer p.deinit(); | 1353 | defer p.deinit(); |
| 1354 | 1354 | ||
| ... | @@ -1364,7 +1364,8 @@ test "json parser dynamic" { | ... | @@ -1364,7 +1364,8 @@ test "json parser dynamic" { |
| 1364 | \\ "Width": 100 | 1364 | \\ "Width": 100 |
| 1365 | \\ }, | 1365 | \\ }, |
| 1366 | \\ "Animated" : false, | 1366 | \\ "Animated" : false, |
| 1367 | \\ "IDs": [116, 943, 234, 38793] | 1367 | \\ "IDs": [116, 943, 234, 38793], |
| 1368 | \\ "ArrayOfObject": [{"n": "m"}] | ||
| 1368 | \\ } | 1369 | \\ } |
| 1369 | \\} | 1370 | \\} |
| 1370 | ; | 1371 | ; |
| ... | @@ -1387,4 +1388,10 @@ test "json parser dynamic" { | ... | @@ -1387,4 +1388,10 @@ test "json parser dynamic" { |
| 1387 | 1388 | ||
| 1388 | const animated = image.Object.get("Animated").?.value; | 1389 | const animated = image.Object.get("Animated").?.value; |
| 1389 | debug.assert(animated.Bool == false); | 1390 | debug.assert(animated.Bool == false); |
| 1391 | |||
| 1392 | const array_of_object = image.Object.get("ArrayOfObject").?.value; | ||
| 1393 | debug.assert(array_of_object.Array.len == 1); | ||
| 1394 | |||
| 1395 | const obj0 = array_of_object.Array.at(0).Object.get("n").?.value; | ||
| 1396 | debug.assert(mem.eql(u8, obj0.String, "m")); | ||
| 1390 | } | 1397 | } |
std/json_test.zig+319-319| ... | @@ -21,7 +21,7 @@ fn any(comptime s: []const u8) void { | ... | @@ -21,7 +21,7 @@ fn any(comptime s: []const u8) void { |
| 21 | // | 21 | // |
| 22 | // Additional tests not part of test JSONTestSuite. | 22 | // Additional tests not part of test JSONTestSuite. |
| 23 | 23 | ||
| 24 | test "y_trailing_comma_after_empty" { | 24 | test "json.test.y_trailing_comma_after_empty" { |
| 25 | ok( | 25 | ok( |
| 26 | \\{"1":[],"2":{},"3":"4"} | 26 | \\{"1":[],"2":{},"3":"4"} |
| 27 | ); | 27 | ); |
| ... | @@ -29,252 +29,252 @@ test "y_trailing_comma_after_empty" { | ... | @@ -29,252 +29,252 @@ test "y_trailing_comma_after_empty" { |
| 29 | 29 | ||
| 30 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 30 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 31 | 31 | ||
| 32 | test "y_array_arraysWithSpaces" { | 32 | test "json.test.y_array_arraysWithSpaces" { |
| 33 | ok( | 33 | ok( |
| 34 | \\[[] ] | 34 | \\[[] ] |
| 35 | ); | 35 | ); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | test "y_array_empty" { | 38 | test "json.test.y_array_empty" { |
| 39 | ok( | 39 | ok( |
| 40 | \\[] | 40 | \\[] |
| 41 | ); | 41 | ); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | test "y_array_empty-string" { | 44 | test "json.test.y_array_empty-string" { |
| 45 | ok( | 45 | ok( |
| 46 | \\[""] | 46 | \\[""] |
| 47 | ); | 47 | ); |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | test "y_array_ending_with_newline" { | 50 | test "json.test.y_array_ending_with_newline" { |
| 51 | ok( | 51 | ok( |
| 52 | \\["a"] | 52 | \\["a"] |
| 53 | ); | 53 | ); |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | test "y_array_false" { | 56 | test "json.test.y_array_false" { |
| 57 | ok( | 57 | ok( |
| 58 | \\[false] | 58 | \\[false] |
| 59 | ); | 59 | ); |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | test "y_array_heterogeneous" { | 62 | test "json.test.y_array_heterogeneous" { |
| 63 | ok( | 63 | ok( |
| 64 | \\[null, 1, "1", {}] | 64 | \\[null, 1, "1", {}] |
| 65 | ); | 65 | ); |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | test "y_array_null" { | 68 | test "json.test.y_array_null" { |
| 69 | ok( | 69 | ok( |
| 70 | \\[null] | 70 | \\[null] |
| 71 | ); | 71 | ); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | test "y_array_with_1_and_newline" { | 74 | test "json.test.y_array_with_1_and_newline" { |
| 75 | ok( | 75 | ok( |
| 76 | \\[1 | 76 | \\[1 |
| 77 | \\] | 77 | \\] |
| 78 | ); | 78 | ); |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | test "y_array_with_leading_space" { | 81 | test "json.test.y_array_with_leading_space" { |
| 82 | ok( | 82 | ok( |
| 83 | \\ [1] | 83 | \\ [1] |
| 84 | ); | 84 | ); |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | test "y_array_with_several_null" { | 87 | test "json.test.y_array_with_several_null" { |
| 88 | ok( | 88 | ok( |
| 89 | \\[1,null,null,null,2] | 89 | \\[1,null,null,null,2] |
| 90 | ); | 90 | ); |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | test "y_array_with_trailing_space" { | 93 | test "json.test.y_array_with_trailing_space" { |
| 94 | ok("[2] "); | 94 | ok("[2] "); |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | test "y_number_0e+1" { | 97 | test "json.test.y_number_0e+1" { |
| 98 | ok( | 98 | ok( |
| 99 | \\[0e+1] | 99 | \\[0e+1] |
| 100 | ); | 100 | ); |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | test "y_number_0e1" { | 103 | test "json.test.y_number_0e1" { |
| 104 | ok( | 104 | ok( |
| 105 | \\[0e1] | 105 | \\[0e1] |
| 106 | ); | 106 | ); |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | test "y_number_after_space" { | 109 | test "json.test.y_number_after_space" { |
| 110 | ok( | 110 | ok( |
| 111 | \\[ 4] | 111 | \\[ 4] |
| 112 | ); | 112 | ); |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | test "y_number_double_close_to_zero" { | 115 | test "json.test.y_number_double_close_to_zero" { |
| 116 | ok( | 116 | ok( |
| 117 | \\[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001] | 117 | \\[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001] |
| 118 | ); | 118 | ); |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | test "y_number_int_with_exp" { | 121 | test "json.test.y_number_int_with_exp" { |
| 122 | ok( | 122 | ok( |
| 123 | \\[20e1] | 123 | \\[20e1] |
| 124 | ); | 124 | ); |
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | test "y_number" { | 127 | test "json.test.y_number" { |
| 128 | ok( | 128 | ok( |
| 129 | \\[123e65] | 129 | \\[123e65] |
| 130 | ); | 130 | ); |
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | test "y_number_minus_zero" { | 133 | test "json.test.y_number_minus_zero" { |
| 134 | ok( | 134 | ok( |
| 135 | \\[-0] | 135 | \\[-0] |
| 136 | ); | 136 | ); |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | test "y_number_negative_int" { | 139 | test "json.test.y_number_negative_int" { |
| 140 | ok( | 140 | ok( |
| 141 | \\[-123] | 141 | \\[-123] |
| 142 | ); | 142 | ); |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | test "y_number_negative_one" { | 145 | test "json.test.y_number_negative_one" { |
| 146 | ok( | 146 | ok( |
| 147 | \\[-1] | 147 | \\[-1] |
| 148 | ); | 148 | ); |
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | test "y_number_negative_zero" { | 151 | test "json.test.y_number_negative_zero" { |
| 152 | ok( | 152 | ok( |
| 153 | \\[-0] | 153 | \\[-0] |
| 154 | ); | 154 | ); |
| 155 | } | 155 | } |
| 156 | 156 | ||
| 157 | test "y_number_real_capital_e" { | 157 | test "json.test.y_number_real_capital_e" { |
| 158 | ok( | 158 | ok( |
| 159 | \\[1E22] | 159 | \\[1E22] |
| 160 | ); | 160 | ); |
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | test "y_number_real_capital_e_neg_exp" { | 163 | test "json.test.y_number_real_capital_e_neg_exp" { |
| 164 | ok( | 164 | ok( |
| 165 | \\[1E-2] | 165 | \\[1E-2] |
| 166 | ); | 166 | ); |
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | test "y_number_real_capital_e_pos_exp" { | 169 | test "json.test.y_number_real_capital_e_pos_exp" { |
| 170 | ok( | 170 | ok( |
| 171 | \\[1E+2] | 171 | \\[1E+2] |
| 172 | ); | 172 | ); |
| 173 | } | 173 | } |
| 174 | 174 | ||
| 175 | test "y_number_real_exponent" { | 175 | test "json.test.y_number_real_exponent" { |
| 176 | ok( | 176 | ok( |
| 177 | \\[123e45] | 177 | \\[123e45] |
| 178 | ); | 178 | ); |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | test "y_number_real_fraction_exponent" { | 181 | test "json.test.y_number_real_fraction_exponent" { |
| 182 | ok( | 182 | ok( |
| 183 | \\[123.456e78] | 183 | \\[123.456e78] |
| 184 | ); | 184 | ); |
| 185 | } | 185 | } |
| 186 | 186 | ||
| 187 | test "y_number_real_neg_exp" { | 187 | test "json.test.y_number_real_neg_exp" { |
| 188 | ok( | 188 | ok( |
| 189 | \\[1e-2] | 189 | \\[1e-2] |
| 190 | ); | 190 | ); |
| 191 | } | 191 | } |
| 192 | 192 | ||
| 193 | test "y_number_real_pos_exponent" { | 193 | test "json.test.y_number_real_pos_exponent" { |
| 194 | ok( | 194 | ok( |
| 195 | \\[1e+2] | 195 | \\[1e+2] |
| 196 | ); | 196 | ); |
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | test "y_number_simple_int" { | 199 | test "json.test.y_number_simple_int" { |
| 200 | ok( | 200 | ok( |
| 201 | \\[123] | 201 | \\[123] |
| 202 | ); | 202 | ); |
| 203 | } | 203 | } |
| 204 | 204 | ||
| 205 | test "y_number_simple_real" { | 205 | test "json.test.y_number_simple_real" { |
| 206 | ok( | 206 | ok( |
| 207 | \\[123.456789] | 207 | \\[123.456789] |
| 208 | ); | 208 | ); |
| 209 | } | 209 | } |
| 210 | 210 | ||
| 211 | test "y_object_basic" { | 211 | test "json.test.y_object_basic" { |
| 212 | ok( | 212 | ok( |
| 213 | \\{"asd":"sdf"} | 213 | \\{"asd":"sdf"} |
| 214 | ); | 214 | ); |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | test "y_object_duplicated_key_and_value" { | 217 | test "json.test.y_object_duplicated_key_and_value" { |
| 218 | ok( | 218 | ok( |
| 219 | \\{"a":"b","a":"b"} | 219 | \\{"a":"b","a":"b"} |
| 220 | ); | 220 | ); |
| 221 | } | 221 | } |
| 222 | 222 | ||
| 223 | test "y_object_duplicated_key" { | 223 | test "json.test.y_object_duplicated_key" { |
| 224 | ok( | 224 | ok( |
| 225 | \\{"a":"b","a":"c"} | 225 | \\{"a":"b","a":"c"} |
| 226 | ); | 226 | ); |
| 227 | } | 227 | } |
| 228 | 228 | ||
| 229 | test "y_object_empty" { | 229 | test "json.test.y_object_empty" { |
| 230 | ok( | 230 | ok( |
| 231 | \\{} | 231 | \\{} |
| 232 | ); | 232 | ); |
| 233 | } | 233 | } |
| 234 | 234 | ||
| 235 | test "y_object_empty_key" { | 235 | test "json.test.y_object_empty_key" { |
| 236 | ok( | 236 | ok( |
| 237 | \\{"":0} | 237 | \\{"":0} |
| 238 | ); | 238 | ); |
| 239 | } | 239 | } |
| 240 | 240 | ||
| 241 | test "y_object_escaped_null_in_key" { | 241 | test "json.test.y_object_escaped_null_in_key" { |
| 242 | ok( | 242 | ok( |
| 243 | \\{"foo\u0000bar": 42} | 243 | \\{"foo\u0000bar": 42} |
| 244 | ); | 244 | ); |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | test "y_object_extreme_numbers" { | 247 | test "json.test.y_object_extreme_numbers" { |
| 248 | ok( | 248 | ok( |
| 249 | \\{ "min": -1.0e+28, "max": 1.0e+28 } | 249 | \\{ "min": -1.0e+28, "max": 1.0e+28 } |
| 250 | ); | 250 | ); |
| 251 | } | 251 | } |
| 252 | 252 | ||
| 253 | test "y_object" { | 253 | test "json.test.y_object" { |
| 254 | ok( | 254 | ok( |
| 255 | \\{"asd":"sdf", "dfg":"fgh"} | 255 | \\{"asd":"sdf", "dfg":"fgh"} |
| 256 | ); | 256 | ); |
| 257 | } | 257 | } |
| 258 | 258 | ||
| 259 | test "y_object_long_strings" { | 259 | test "json.test.y_object_long_strings" { |
| 260 | ok( | 260 | ok( |
| 261 | \\{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} | 261 | \\{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} |
| 262 | ); | 262 | ); |
| 263 | } | 263 | } |
| 264 | 264 | ||
| 265 | test "y_object_simple" { | 265 | test "json.test.y_object_simple" { |
| 266 | ok( | 266 | ok( |
| 267 | \\{"a":[]} | 267 | \\{"a":[]} |
| 268 | ); | 268 | ); |
| 269 | } | 269 | } |
| 270 | 270 | ||
| 271 | test "y_object_string_unicode" { | 271 | test "json.test.y_object_string_unicode" { |
| 272 | ok( | 272 | ok( |
| 273 | \\{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" } | 273 | \\{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" } |
| 274 | ); | 274 | ); |
| 275 | } | 275 | } |
| 276 | 276 | ||
| 277 | test "y_object_with_newlines" { | 277 | test "json.test.y_object_with_newlines" { |
| 278 | ok( | 278 | ok( |
| 279 | \\{ | 279 | \\{ |
| 280 | \\"a": "b" | 280 | \\"a": "b" |
| ... | @@ -282,419 +282,419 @@ test "y_object_with_newlines" { | ... | @@ -282,419 +282,419 @@ test "y_object_with_newlines" { |
| 282 | ); | 282 | ); |
| 283 | } | 283 | } |
| 284 | 284 | ||
| 285 | test "y_string_1_2_3_bytes_UTF-8_sequences" { | 285 | test "json.test.y_string_1_2_3_bytes_UTF-8_sequences" { |
| 286 | ok( | 286 | ok( |
| 287 | \\["\u0060\u012a\u12AB"] | 287 | \\["\u0060\u012a\u12AB"] |
| 288 | ); | 288 | ); |
| 289 | } | 289 | } |
| 290 | 290 | ||
| 291 | test "y_string_accepted_surrogate_pair" { | 291 | test "json.test.y_string_accepted_surrogate_pair" { |
| 292 | ok( | 292 | ok( |
| 293 | \\["\uD801\udc37"] | 293 | \\["\uD801\udc37"] |
| 294 | ); | 294 | ); |
| 295 | } | 295 | } |
| 296 | 296 | ||
| 297 | test "y_string_accepted_surrogate_pairs" { | 297 | test "json.test.y_string_accepted_surrogate_pairs" { |
| 298 | ok( | 298 | ok( |
| 299 | \\["\ud83d\ude39\ud83d\udc8d"] | 299 | \\["\ud83d\ude39\ud83d\udc8d"] |
| 300 | ); | 300 | ); |
| 301 | } | 301 | } |
| 302 | 302 | ||
| 303 | test "y_string_allowed_escapes" { | 303 | test "json.test.y_string_allowed_escapes" { |
| 304 | ok( | 304 | ok( |
| 305 | \\["\"\\\/\b\f\n\r\t"] | 305 | \\["\"\\\/\b\f\n\r\t"] |
| 306 | ); | 306 | ); |
| 307 | } | 307 | } |
| 308 | 308 | ||
| 309 | test "y_string_backslash_and_u_escaped_zero" { | 309 | test "json.test.y_string_backslash_and_u_escaped_zero" { |
| 310 | ok( | 310 | ok( |
| 311 | \\["\\u0000"] | 311 | \\["\\u0000"] |
| 312 | ); | 312 | ); |
| 313 | } | 313 | } |
| 314 | 314 | ||
| 315 | test "y_string_backslash_doublequotes" { | 315 | test "json.test.y_string_backslash_doublequotes" { |
| 316 | ok( | 316 | ok( |
| 317 | \\["\""] | 317 | \\["\""] |
| 318 | ); | 318 | ); |
| 319 | } | 319 | } |
| 320 | 320 | ||
| 321 | test "y_string_comments" { | 321 | test "json.test.y_string_comments" { |
| 322 | ok( | 322 | ok( |
| 323 | \\["a/*b*/c/*d//e"] | 323 | \\["a/*b*/c/*d//e"] |
| 324 | ); | 324 | ); |
| 325 | } | 325 | } |
| 326 | 326 | ||
| 327 | test "y_string_double_escape_a" { | 327 | test "json.test.y_string_double_escape_a" { |
| 328 | ok( | 328 | ok( |
| 329 | \\["\\a"] | 329 | \\["\\a"] |
| 330 | ); | 330 | ); |
| 331 | } | 331 | } |
| 332 | 332 | ||
| 333 | test "y_string_double_escape_n" { | 333 | test "json.test.y_string_double_escape_n" { |
| 334 | ok( | 334 | ok( |
| 335 | \\["\\n"] | 335 | \\["\\n"] |
| 336 | ); | 336 | ); |
| 337 | } | 337 | } |
| 338 | 338 | ||
| 339 | test "y_string_escaped_control_character" { | 339 | test "json.test.y_string_escaped_control_character" { |
| 340 | ok( | 340 | ok( |
| 341 | \\["\u0012"] | 341 | \\["\u0012"] |
| 342 | ); | 342 | ); |
| 343 | } | 343 | } |
| 344 | 344 | ||
| 345 | test "y_string_escaped_noncharacter" { | 345 | test "json.test.y_string_escaped_noncharacter" { |
| 346 | ok( | 346 | ok( |
| 347 | \\["\uFFFF"] | 347 | \\["\uFFFF"] |
| 348 | ); | 348 | ); |
| 349 | } | 349 | } |
| 350 | 350 | ||
| 351 | test "y_string_in_array" { | 351 | test "json.test.y_string_in_array" { |
| 352 | ok( | 352 | ok( |
| 353 | \\["asd"] | 353 | \\["asd"] |
| 354 | ); | 354 | ); |
| 355 | } | 355 | } |
| 356 | 356 | ||
| 357 | test "y_string_in_array_with_leading_space" { | 357 | test "json.test.y_string_in_array_with_leading_space" { |
| 358 | ok( | 358 | ok( |
| 359 | \\[ "asd"] | 359 | \\[ "asd"] |
| 360 | ); | 360 | ); |
| 361 | } | 361 | } |
| 362 | 362 | ||
| 363 | test "y_string_last_surrogates_1_and_2" { | 363 | test "json.test.y_string_last_surrogates_1_and_2" { |
| 364 | ok( | 364 | ok( |
| 365 | \\["\uDBFF\uDFFF"] | 365 | \\["\uDBFF\uDFFF"] |
| 366 | ); | 366 | ); |
| 367 | } | 367 | } |
| 368 | 368 | ||
| 369 | test "y_string_nbsp_uescaped" { | 369 | test "json.test.y_string_nbsp_uescaped" { |
| 370 | ok( | 370 | ok( |
| 371 | \\["new\u00A0line"] | 371 | \\["new\u00A0line"] |
| 372 | ); | 372 | ); |
| 373 | } | 373 | } |
| 374 | 374 | ||
| 375 | test "y_string_nonCharacterInUTF-8_U+10FFFF" { | 375 | test "json.test.y_string_nonCharacterInUTF-8_U+10FFFF" { |
| 376 | ok( | 376 | ok( |
| 377 | \\[""] | 377 | \\[""] |
| 378 | ); | 378 | ); |
| 379 | } | 379 | } |
| 380 | 380 | ||
| 381 | test "y_string_nonCharacterInUTF-8_U+FFFF" { | 381 | test "json.test.y_string_nonCharacterInUTF-8_U+FFFF" { |
| 382 | ok( | 382 | ok( |
| 383 | \\[""] | 383 | \\[""] |
| 384 | ); | 384 | ); |
| 385 | } | 385 | } |
| 386 | 386 | ||
| 387 | test "y_string_null_escape" { | 387 | test "json.test.y_string_null_escape" { |
| 388 | ok( | 388 | ok( |
| 389 | \\["\u0000"] | 389 | \\["\u0000"] |
| 390 | ); | 390 | ); |
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | test "y_string_one-byte-utf-8" { | 393 | test "json.test.y_string_one-byte-utf-8" { |
| 394 | ok( | 394 | ok( |
| 395 | \\["\u002c"] | 395 | \\["\u002c"] |
| 396 | ); | 396 | ); |
| 397 | } | 397 | } |
| 398 | 398 | ||
| 399 | test "y_string_pi" { | 399 | test "json.test.y_string_pi" { |
| 400 | ok( | 400 | ok( |
| 401 | \\["π"] | 401 | \\["π"] |
| 402 | ); | 402 | ); |
| 403 | } | 403 | } |
| 404 | 404 | ||
| 405 | test "y_string_reservedCharacterInUTF-8_U+1BFFF" { | 405 | test "json.test.y_string_reservedCharacterInUTF-8_U+1BFFF" { |
| 406 | ok( | 406 | ok( |
| 407 | \\[""] | 407 | \\[""] |
| 408 | ); | 408 | ); |
| 409 | } | 409 | } |
| 410 | 410 | ||
| 411 | test "y_string_simple_ascii" { | 411 | test "json.test.y_string_simple_ascii" { |
| 412 | ok( | 412 | ok( |
| 413 | \\["asd "] | 413 | \\["asd "] |
| 414 | ); | 414 | ); |
| 415 | } | 415 | } |
| 416 | 416 | ||
| 417 | test "y_string_space" { | 417 | test "json.test.y_string_space" { |
| 418 | ok( | 418 | ok( |
| 419 | \\" " | 419 | \\" " |
| 420 | ); | 420 | ); |
| 421 | } | 421 | } |
| 422 | 422 | ||
| 423 | test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" { | 423 | test "json.test.y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" { |
| 424 | ok( | 424 | ok( |
| 425 | \\["\uD834\uDd1e"] | 425 | \\["\uD834\uDd1e"] |
| 426 | ); | 426 | ); |
| 427 | } | 427 | } |
| 428 | 428 | ||
| 429 | test "y_string_three-byte-utf-8" { | 429 | test "json.test.y_string_three-byte-utf-8" { |
| 430 | ok( | 430 | ok( |
| 431 | \\["\u0821"] | 431 | \\["\u0821"] |
| 432 | ); | 432 | ); |
| 433 | } | 433 | } |
| 434 | 434 | ||
| 435 | test "y_string_two-byte-utf-8" { | 435 | test "json.test.y_string_two-byte-utf-8" { |
| 436 | ok( | 436 | ok( |
| 437 | \\["\u0123"] | 437 | \\["\u0123"] |
| 438 | ); | 438 | ); |
| 439 | } | 439 | } |
| 440 | 440 | ||
| 441 | test "y_string_u+2028_line_sep" { | 441 | test "json.test.y_string_u+2028_line_sep" { |
| 442 | ok("[\"\xe2\x80\xa8\"]"); | 442 | ok("[\"\xe2\x80\xa8\"]"); |
| 443 | } | 443 | } |
| 444 | 444 | ||
| 445 | test "y_string_u+2029_par_sep" { | 445 | test "json.test.y_string_u+2029_par_sep" { |
| 446 | ok("[\"\xe2\x80\xa9\"]"); | 446 | ok("[\"\xe2\x80\xa9\"]"); |
| 447 | } | 447 | } |
| 448 | 448 | ||
| 449 | test "y_string_uescaped_newline" { | 449 | test "json.test.y_string_uescaped_newline" { |
| 450 | ok( | 450 | ok( |
| 451 | \\["new\u000Aline"] | 451 | \\["new\u000Aline"] |
| 452 | ); | 452 | ); |
| 453 | } | 453 | } |
| 454 | 454 | ||
| 455 | test "y_string_uEscape" { | 455 | test "json.test.y_string_uEscape" { |
| 456 | ok( | 456 | ok( |
| 457 | \\["\u0061\u30af\u30EA\u30b9"] | 457 | \\["\u0061\u30af\u30EA\u30b9"] |
| 458 | ); | 458 | ); |
| 459 | } | 459 | } |
| 460 | 460 | ||
| 461 | test "y_string_unescaped_char_delete" { | 461 | test "json.test.y_string_unescaped_char_delete" { |
| 462 | ok("[\"\x7f\"]"); | 462 | ok("[\"\x7f\"]"); |
| 463 | } | 463 | } |
| 464 | 464 | ||
| 465 | test "y_string_unicode_2" { | 465 | test "json.test.y_string_unicode_2" { |
| 466 | ok( | 466 | ok( |
| 467 | \\["⍂㈴⍂"] | 467 | \\["⍂㈴⍂"] |
| 468 | ); | 468 | ); |
| 469 | } | 469 | } |
| 470 | 470 | ||
| 471 | test "y_string_unicodeEscapedBackslash" { | 471 | test "json.test.y_string_unicodeEscapedBackslash" { |
| 472 | ok( | 472 | ok( |
| 473 | \\["\u005C"] | 473 | \\["\u005C"] |
| 474 | ); | 474 | ); |
| 475 | } | 475 | } |
| 476 | 476 | ||
| 477 | test "y_string_unicode_escaped_double_quote" { | 477 | test "json.test.y_string_unicode_escaped_double_quote" { |
| 478 | ok( | 478 | ok( |
| 479 | \\["\u0022"] | 479 | \\["\u0022"] |
| 480 | ); | 480 | ); |
| 481 | } | 481 | } |
| 482 | 482 | ||
| 483 | test "y_string_unicode" { | 483 | test "json.test.y_string_unicode" { |
| 484 | ok( | 484 | ok( |
| 485 | \\["\uA66D"] | 485 | \\["\uA66D"] |
| 486 | ); | 486 | ); |
| 487 | } | 487 | } |
| 488 | 488 | ||
| 489 | test "y_string_unicode_U+10FFFE_nonchar" { | 489 | test "json.test.y_string_unicode_U+10FFFE_nonchar" { |
| 490 | ok( | 490 | ok( |
| 491 | \\["\uDBFF\uDFFE"] | 491 | \\["\uDBFF\uDFFE"] |
| 492 | ); | 492 | ); |
| 493 | } | 493 | } |
| 494 | 494 | ||
| 495 | test "y_string_unicode_U+1FFFE_nonchar" { | 495 | test "json.test.y_string_unicode_U+1FFFE_nonchar" { |
| 496 | ok( | 496 | ok( |
| 497 | \\["\uD83F\uDFFE"] | 497 | \\["\uD83F\uDFFE"] |
| 498 | ); | 498 | ); |
| 499 | } | 499 | } |
| 500 | 500 | ||
| 501 | test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" { | 501 | test "json.test.y_string_unicode_U+200B_ZERO_WIDTH_SPACE" { |
| 502 | ok( | 502 | ok( |
| 503 | \\["\u200B"] | 503 | \\["\u200B"] |
| 504 | ); | 504 | ); |
| 505 | } | 505 | } |
| 506 | 506 | ||
| 507 | test "y_string_unicode_U+2064_invisible_plus" { | 507 | test "json.test.y_string_unicode_U+2064_invisible_plus" { |
| 508 | ok( | 508 | ok( |
| 509 | \\["\u2064"] | 509 | \\["\u2064"] |
| 510 | ); | 510 | ); |
| 511 | } | 511 | } |
| 512 | 512 | ||
| 513 | test "y_string_unicode_U+FDD0_nonchar" { | 513 | test "json.test.y_string_unicode_U+FDD0_nonchar" { |
| 514 | ok( | 514 | ok( |
| 515 | \\["\uFDD0"] | 515 | \\["\uFDD0"] |
| 516 | ); | 516 | ); |
| 517 | } | 517 | } |
| 518 | 518 | ||
| 519 | test "y_string_unicode_U+FFFE_nonchar" { | 519 | test "json.test.y_string_unicode_U+FFFE_nonchar" { |
| 520 | ok( | 520 | ok( |
| 521 | \\["\uFFFE"] | 521 | \\["\uFFFE"] |
| 522 | ); | 522 | ); |
| 523 | } | 523 | } |
| 524 | 524 | ||
| 525 | test "y_string_utf8" { | 525 | test "json.test.y_string_utf8" { |
| 526 | ok( | 526 | ok( |
| 527 | \\["€𝄞"] | 527 | \\["€𝄞"] |
| 528 | ); | 528 | ); |
| 529 | } | 529 | } |
| 530 | 530 | ||
| 531 | test "y_string_with_del_character" { | 531 | test "json.test.y_string_with_del_character" { |
| 532 | ok("[\"a\x7fa\"]"); | 532 | ok("[\"a\x7fa\"]"); |
| 533 | } | 533 | } |
| 534 | 534 | ||
| 535 | test "y_structure_lonely_false" { | 535 | test "json.test.y_structure_lonely_false" { |
| 536 | ok( | 536 | ok( |
| 537 | \\false | 537 | \\false |
| 538 | ); | 538 | ); |
| 539 | } | 539 | } |
| 540 | 540 | ||
| 541 | test "y_structure_lonely_int" { | 541 | test "json.test.y_structure_lonely_int" { |
| 542 | ok( | 542 | ok( |
| 543 | \\42 | 543 | \\42 |
| 544 | ); | 544 | ); |
| 545 | } | 545 | } |
| 546 | 546 | ||
| 547 | test "y_structure_lonely_negative_real" { | 547 | test "json.test.y_structure_lonely_negative_real" { |
| 548 | ok( | 548 | ok( |
| 549 | \\-0.1 | 549 | \\-0.1 |
| 550 | ); | 550 | ); |
| 551 | } | 551 | } |
| 552 | 552 | ||
| 553 | test "y_structure_lonely_null" { | 553 | test "json.test.y_structure_lonely_null" { |
| 554 | ok( | 554 | ok( |
| 555 | \\null | 555 | \\null |
| 556 | ); | 556 | ); |
| 557 | } | 557 | } |
| 558 | 558 | ||
| 559 | test "y_structure_lonely_string" { | 559 | test "json.test.y_structure_lonely_string" { |
| 560 | ok( | 560 | ok( |
| 561 | \\"asd" | 561 | \\"asd" |
| 562 | ); | 562 | ); |
| 563 | } | 563 | } |
| 564 | 564 | ||
| 565 | test "y_structure_lonely_true" { | 565 | test "json.test.y_structure_lonely_true" { |
| 566 | ok( | 566 | ok( |
| 567 | \\true | 567 | \\true |
| 568 | ); | 568 | ); |
| 569 | } | 569 | } |
| 570 | 570 | ||
| 571 | test "y_structure_string_empty" { | 571 | test "json.test.y_structure_string_empty" { |
| 572 | ok( | 572 | ok( |
| 573 | \\"" | 573 | \\"" |
| 574 | ); | 574 | ); |
| 575 | } | 575 | } |
| 576 | 576 | ||
| 577 | test "y_structure_trailing_newline" { | 577 | test "json.test.y_structure_trailing_newline" { |
| 578 | ok( | 578 | ok( |
| 579 | \\["a"] | 579 | \\["a"] |
| 580 | ); | 580 | ); |
| 581 | } | 581 | } |
| 582 | 582 | ||
| 583 | test "y_structure_true_in_array" { | 583 | test "json.test.y_structure_true_in_array" { |
| 584 | ok( | 584 | ok( |
| 585 | \\[true] | 585 | \\[true] |
| 586 | ); | 586 | ); |
| 587 | } | 587 | } |
| 588 | 588 | ||
| 589 | test "y_structure_whitespace_array" { | 589 | test "json.test.y_structure_whitespace_array" { |
| 590 | ok(" [] "); | 590 | ok(" [] "); |
| 591 | } | 591 | } |
| 592 | 592 | ||
| 593 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 593 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 594 | 594 | ||
| 595 | test "n_array_1_true_without_comma" { | 595 | test "json.test.n_array_1_true_without_comma" { |
| 596 | err( | 596 | err( |
| 597 | \\[1 true] | 597 | \\[1 true] |
| 598 | ); | 598 | ); |
| 599 | } | 599 | } |
| 600 | 600 | ||
| 601 | test "n_array_a_invalid_utf8" { | 601 | test "json.test.n_array_a_invalid_utf8" { |
| 602 | err( | 602 | err( |
| 603 | \\[aå] | 603 | \\[aå] |
| 604 | ); | 604 | ); |
| 605 | } | 605 | } |
| 606 | 606 | ||
| 607 | test "n_array_colon_instead_of_comma" { | 607 | test "json.test.n_array_colon_instead_of_comma" { |
| 608 | err( | 608 | err( |
| 609 | \\["": 1] | 609 | \\["": 1] |
| 610 | ); | 610 | ); |
| 611 | } | 611 | } |
| 612 | 612 | ||
| 613 | test "n_array_comma_after_close" { | 613 | test "json.test.n_array_comma_after_close" { |
| 614 | //err( | 614 | //err( |
| 615 | // \\[""], | 615 | // \\[""], |
| 616 | //); | 616 | //); |
| 617 | } | 617 | } |
| 618 | 618 | ||
| 619 | test "n_array_comma_and_number" { | 619 | test "json.test.n_array_comma_and_number" { |
| 620 | err( | 620 | err( |
| 621 | \\[,1] | 621 | \\[,1] |
| 622 | ); | 622 | ); |
| 623 | } | 623 | } |
| 624 | 624 | ||
| 625 | test "n_array_double_comma" { | 625 | test "json.test.n_array_double_comma" { |
| 626 | err( | 626 | err( |
| 627 | \\[1,,2] | 627 | \\[1,,2] |
| 628 | ); | 628 | ); |
| 629 | } | 629 | } |
| 630 | 630 | ||
| 631 | test "n_array_double_extra_comma" { | 631 | test "json.test.n_array_double_extra_comma" { |
| 632 | err( | 632 | err( |
| 633 | \\["x",,] | 633 | \\["x",,] |
| 634 | ); | 634 | ); |
| 635 | } | 635 | } |
| 636 | 636 | ||
| 637 | test "n_array_extra_close" { | 637 | test "json.test.n_array_extra_close" { |
| 638 | err( | 638 | err( |
| 639 | \\["x"]] | 639 | \\["x"]] |
| 640 | ); | 640 | ); |
| 641 | } | 641 | } |
| 642 | 642 | ||
| 643 | test "n_array_extra_comma" { | 643 | test "json.test.n_array_extra_comma" { |
| 644 | //err( | 644 | //err( |
| 645 | // \\["",] | 645 | // \\["",] |
| 646 | //); | 646 | //); |
| 647 | } | 647 | } |
| 648 | 648 | ||
| 649 | test "n_array_incomplete_invalid_value" { | 649 | test "json.test.n_array_incomplete_invalid_value" { |
| 650 | err( | 650 | err( |
| 651 | \\[x | 651 | \\[x |
| 652 | ); | 652 | ); |
| 653 | } | 653 | } |
| 654 | 654 | ||
| 655 | test "n_array_incomplete" { | 655 | test "json.test.n_array_incomplete" { |
| 656 | err( | 656 | err( |
| 657 | \\["x" | 657 | \\["x" |
| 658 | ); | 658 | ); |
| 659 | } | 659 | } |
| 660 | 660 | ||
| 661 | test "n_array_inner_array_no_comma" { | 661 | test "json.test.n_array_inner_array_no_comma" { |
| 662 | err( | 662 | err( |
| 663 | \\[3[4]] | 663 | \\[3[4]] |
| 664 | ); | 664 | ); |
| 665 | } | 665 | } |
| 666 | 666 | ||
| 667 | test "n_array_invalid_utf8" { | 667 | test "json.test.n_array_invalid_utf8" { |
| 668 | err( | 668 | err( |
| 669 | \\[ÿ] | 669 | \\[ÿ] |
| 670 | ); | 670 | ); |
| 671 | } | 671 | } |
| 672 | 672 | ||
| 673 | test "n_array_items_separated_by_semicolon" { | 673 | test "json.test.n_array_items_separated_by_semicolon" { |
| 674 | err( | 674 | err( |
| 675 | \\[1:2] | 675 | \\[1:2] |
| 676 | ); | 676 | ); |
| 677 | } | 677 | } |
| 678 | 678 | ||
| 679 | test "n_array_just_comma" { | 679 | test "json.test.n_array_just_comma" { |
| 680 | err( | 680 | err( |
| 681 | \\[,] | 681 | \\[,] |
| 682 | ); | 682 | ); |
| 683 | } | 683 | } |
| 684 | 684 | ||
| 685 | test "n_array_just_minus" { | 685 | test "json.test.n_array_just_minus" { |
| 686 | err( | 686 | err( |
| 687 | \\[-] | 687 | \\[-] |
| 688 | ); | 688 | ); |
| 689 | } | 689 | } |
| 690 | 690 | ||
| 691 | test "n_array_missing_value" { | 691 | test "json.test.n_array_missing_value" { |
| 692 | err( | 692 | err( |
| 693 | \\[ , ""] | 693 | \\[ , ""] |
| 694 | ); | 694 | ); |
| 695 | } | 695 | } |
| 696 | 696 | ||
| 697 | test "n_array_newlines_unclosed" { | 697 | test "json.test.n_array_newlines_unclosed" { |
| 698 | err( | 698 | err( |
| 699 | \\["a", | 699 | \\["a", |
| 700 | \\4 | 700 | \\4 |
| ... | @@ -702,41 +702,41 @@ test "n_array_newlines_unclosed" { | ... | @@ -702,41 +702,41 @@ test "n_array_newlines_unclosed" { |
| 702 | ); | 702 | ); |
| 703 | } | 703 | } |
| 704 | 704 | ||
| 705 | test "n_array_number_and_comma" { | 705 | test "json.test.n_array_number_and_comma" { |
| 706 | err( | 706 | err( |
| 707 | \\[1,] | 707 | \\[1,] |
| 708 | ); | 708 | ); |
| 709 | } | 709 | } |
| 710 | 710 | ||
| 711 | test "n_array_number_and_several_commas" { | 711 | test "json.test.n_array_number_and_several_commas" { |
| 712 | err( | 712 | err( |
| 713 | \\[1,,] | 713 | \\[1,,] |
| 714 | ); | 714 | ); |
| 715 | } | 715 | } |
| 716 | 716 | ||
| 717 | test "n_array_spaces_vertical_tab_formfeed" { | 717 | test "json.test.n_array_spaces_vertical_tab_formfeed" { |
| 718 | err("[\"\x0aa\"\\f]"); | 718 | err("[\"\x0aa\"\\f]"); |
| 719 | } | 719 | } |
| 720 | 720 | ||
| 721 | test "n_array_star_inside" { | 721 | test "json.test.n_array_star_inside" { |
| 722 | err( | 722 | err( |
| 723 | \\[*] | 723 | \\[*] |
| 724 | ); | 724 | ); |
| 725 | } | 725 | } |
| 726 | 726 | ||
| 727 | test "n_array_unclosed" { | 727 | test "json.test.n_array_unclosed" { |
| 728 | err( | 728 | err( |
| 729 | \\["" | 729 | \\["" |
| 730 | ); | 730 | ); |
| 731 | } | 731 | } |
| 732 | 732 | ||
| 733 | test "n_array_unclosed_trailing_comma" { | 733 | test "json.test.n_array_unclosed_trailing_comma" { |
| 734 | err( | 734 | err( |
| 735 | \\[1, | 735 | \\[1, |
| 736 | ); | 736 | ); |
| 737 | } | 737 | } |
| 738 | 738 | ||
| 739 | test "n_array_unclosed_with_new_lines" { | 739 | test "json.test.n_array_unclosed_with_new_lines" { |
| 740 | err( | 740 | err( |
| 741 | \\[1, | 741 | \\[1, |
| 742 | \\1 | 742 | \\1 |
| ... | @@ -744,956 +744,956 @@ test "n_array_unclosed_with_new_lines" { | ... | @@ -744,956 +744,956 @@ test "n_array_unclosed_with_new_lines" { |
| 744 | ); | 744 | ); |
| 745 | } | 745 | } |
| 746 | 746 | ||
| 747 | test "n_array_unclosed_with_object_inside" { | 747 | test "json.test.n_array_unclosed_with_object_inside" { |
| 748 | err( | 748 | err( |
| 749 | \\[{} | 749 | \\[{} |
| 750 | ); | 750 | ); |
| 751 | } | 751 | } |
| 752 | 752 | ||
| 753 | test "n_incomplete_false" { | 753 | test "json.test.n_incomplete_false" { |
| 754 | err( | 754 | err( |
| 755 | \\[fals] | 755 | \\[fals] |
| 756 | ); | 756 | ); |
| 757 | } | 757 | } |
| 758 | 758 | ||
| 759 | test "n_incomplete_null" { | 759 | test "json.test.n_incomplete_null" { |
| 760 | err( | 760 | err( |
| 761 | \\[nul] | 761 | \\[nul] |
| 762 | ); | 762 | ); |
| 763 | } | 763 | } |
| 764 | 764 | ||
| 765 | test "n_incomplete_true" { | 765 | test "json.test.n_incomplete_true" { |
| 766 | err( | 766 | err( |
| 767 | \\[tru] | 767 | \\[tru] |
| 768 | ); | 768 | ); |
| 769 | } | 769 | } |
| 770 | 770 | ||
| 771 | test "n_multidigit_number_then_00" { | 771 | test "json.test.n_multidigit_number_then_00" { |
| 772 | err("123\x00"); | 772 | err("123\x00"); |
| 773 | } | 773 | } |
| 774 | 774 | ||
| 775 | test "n_number_0.1.2" { | 775 | test "json.test.n_number_0.1.2" { |
| 776 | err( | 776 | err( |
| 777 | \\[0.1.2] | 777 | \\[0.1.2] |
| 778 | ); | 778 | ); |
| 779 | } | 779 | } |
| 780 | 780 | ||
| 781 | test "n_number_-01" { | 781 | test "json.test.n_number_-01" { |
| 782 | err( | 782 | err( |
| 783 | \\[-01] | 783 | \\[-01] |
| 784 | ); | 784 | ); |
| 785 | } | 785 | } |
| 786 | 786 | ||
| 787 | test "n_number_0.3e" { | 787 | test "json.test.n_number_0.3e" { |
| 788 | err( | 788 | err( |
| 789 | \\[0.3e] | 789 | \\[0.3e] |
| 790 | ); | 790 | ); |
| 791 | } | 791 | } |
| 792 | 792 | ||
| 793 | test "n_number_0.3e+" { | 793 | test "json.test.n_number_0.3e+" { |
| 794 | err( | 794 | err( |
| 795 | \\[0.3e+] | 795 | \\[0.3e+] |
| 796 | ); | 796 | ); |
| 797 | } | 797 | } |
| 798 | 798 | ||
| 799 | test "n_number_0_capital_E" { | 799 | test "json.test.n_number_0_capital_E" { |
| 800 | err( | 800 | err( |
| 801 | \\[0E] | 801 | \\[0E] |
| 802 | ); | 802 | ); |
| 803 | } | 803 | } |
| 804 | 804 | ||
| 805 | test "n_number_0_capital_E+" { | 805 | test "json.test.n_number_0_capital_E+" { |
| 806 | err( | 806 | err( |
| 807 | \\[0E+] | 807 | \\[0E+] |
| 808 | ); | 808 | ); |
| 809 | } | 809 | } |
| 810 | 810 | ||
| 811 | test "n_number_0.e1" { | 811 | test "json.test.n_number_0.e1" { |
| 812 | err( | 812 | err( |
| 813 | \\[0.e1] | 813 | \\[0.e1] |
| 814 | ); | 814 | ); |
| 815 | } | 815 | } |
| 816 | 816 | ||
| 817 | test "n_number_0e" { | 817 | test "json.test.n_number_0e" { |
| 818 | err( | 818 | err( |
| 819 | \\[0e] | 819 | \\[0e] |
| 820 | ); | 820 | ); |
| 821 | } | 821 | } |
| 822 | 822 | ||
| 823 | test "n_number_0e+" { | 823 | test "json.test.n_number_0e+" { |
| 824 | err( | 824 | err( |
| 825 | \\[0e+] | 825 | \\[0e+] |
| 826 | ); | 826 | ); |
| 827 | } | 827 | } |
| 828 | 828 | ||
| 829 | test "n_number_1_000" { | 829 | test "json.test.n_number_1_000" { |
| 830 | err( | 830 | err( |
| 831 | \\[1 000.0] | 831 | \\[1 000.0] |
| 832 | ); | 832 | ); |
| 833 | } | 833 | } |
| 834 | 834 | ||
| 835 | test "n_number_1.0e-" { | 835 | test "json.test.n_number_1.0e-" { |
| 836 | err( | 836 | err( |
| 837 | \\[1.0e-] | 837 | \\[1.0e-] |
| 838 | ); | 838 | ); |
| 839 | } | 839 | } |
| 840 | 840 | ||
| 841 | test "n_number_1.0e" { | 841 | test "json.test.n_number_1.0e" { |
| 842 | err( | 842 | err( |
| 843 | \\[1.0e] | 843 | \\[1.0e] |
| 844 | ); | 844 | ); |
| 845 | } | 845 | } |
| 846 | 846 | ||
| 847 | test "n_number_1.0e+" { | 847 | test "json.test.n_number_1.0e+" { |
| 848 | err( | 848 | err( |
| 849 | \\[1.0e+] | 849 | \\[1.0e+] |
| 850 | ); | 850 | ); |
| 851 | } | 851 | } |
| 852 | 852 | ||
| 853 | test "n_number_-1.0." { | 853 | test "json.test.n_number_-1.0." { |
| 854 | err( | 854 | err( |
| 855 | \\[-1.0.] | 855 | \\[-1.0.] |
| 856 | ); | 856 | ); |
| 857 | } | 857 | } |
| 858 | 858 | ||
| 859 | test "n_number_1eE2" { | 859 | test "json.test.n_number_1eE2" { |
| 860 | err( | 860 | err( |
| 861 | \\[1eE2] | 861 | \\[1eE2] |
| 862 | ); | 862 | ); |
| 863 | } | 863 | } |
| 864 | 864 | ||
| 865 | test "n_number_.-1" { | 865 | test "json.test.n_number_.-1" { |
| 866 | err( | 866 | err( |
| 867 | \\[.-1] | 867 | \\[.-1] |
| 868 | ); | 868 | ); |
| 869 | } | 869 | } |
| 870 | 870 | ||
| 871 | test "n_number_+1" { | 871 | test "json.test.n_number_+1" { |
| 872 | err( | 872 | err( |
| 873 | \\[+1] | 873 | \\[+1] |
| 874 | ); | 874 | ); |
| 875 | } | 875 | } |
| 876 | 876 | ||
| 877 | test "n_number_.2e-3" { | 877 | test "json.test.n_number_.2e-3" { |
| 878 | err( | 878 | err( |
| 879 | \\[.2e-3] | 879 | \\[.2e-3] |
| 880 | ); | 880 | ); |
| 881 | } | 881 | } |
| 882 | 882 | ||
| 883 | test "n_number_2.e-3" { | 883 | test "json.test.n_number_2.e-3" { |
| 884 | err( | 884 | err( |
| 885 | \\[2.e-3] | 885 | \\[2.e-3] |
| 886 | ); | 886 | ); |
| 887 | } | 887 | } |
| 888 | 888 | ||
| 889 | test "n_number_2.e+3" { | 889 | test "json.test.n_number_2.e+3" { |
| 890 | err( | 890 | err( |
| 891 | \\[2.e+3] | 891 | \\[2.e+3] |
| 892 | ); | 892 | ); |
| 893 | } | 893 | } |
| 894 | 894 | ||
| 895 | test "n_number_2.e3" { | 895 | test "json.test.n_number_2.e3" { |
| 896 | err( | 896 | err( |
| 897 | \\[2.e3] | 897 | \\[2.e3] |
| 898 | ); | 898 | ); |
| 899 | } | 899 | } |
| 900 | 900 | ||
| 901 | test "n_number_-2." { | 901 | test "json.test.n_number_-2." { |
| 902 | err( | 902 | err( |
| 903 | \\[-2.] | 903 | \\[-2.] |
| 904 | ); | 904 | ); |
| 905 | } | 905 | } |
| 906 | 906 | ||
| 907 | test "n_number_9.e+" { | 907 | test "json.test.n_number_9.e+" { |
| 908 | err( | 908 | err( |
| 909 | \\[9.e+] | 909 | \\[9.e+] |
| 910 | ); | 910 | ); |
| 911 | } | 911 | } |
| 912 | 912 | ||
| 913 | test "n_number_expression" { | 913 | test "json.test.n_number_expression" { |
| 914 | err( | 914 | err( |
| 915 | \\[1+2] | 915 | \\[1+2] |
| 916 | ); | 916 | ); |
| 917 | } | 917 | } |
| 918 | 918 | ||
| 919 | test "n_number_hex_1_digit" { | 919 | test "json.test.n_number_hex_1_digit" { |
| 920 | err( | 920 | err( |
| 921 | \\[0x1] | 921 | \\[0x1] |
| 922 | ); | 922 | ); |
| 923 | } | 923 | } |
| 924 | 924 | ||
| 925 | test "n_number_hex_2_digits" { | 925 | test "json.test.n_number_hex_2_digits" { |
| 926 | err( | 926 | err( |
| 927 | \\[0x42] | 927 | \\[0x42] |
| 928 | ); | 928 | ); |
| 929 | } | 929 | } |
| 930 | 930 | ||
| 931 | test "n_number_infinity" { | 931 | test "json.test.n_number_infinity" { |
| 932 | err( | 932 | err( |
| 933 | \\[Infinity] | 933 | \\[Infinity] |
| 934 | ); | 934 | ); |
| 935 | } | 935 | } |
| 936 | 936 | ||
| 937 | test "n_number_+Inf" { | 937 | test "json.test.n_number_+Inf" { |
| 938 | err( | 938 | err( |
| 939 | \\[+Inf] | 939 | \\[+Inf] |
| 940 | ); | 940 | ); |
| 941 | } | 941 | } |
| 942 | 942 | ||
| 943 | test "n_number_Inf" { | 943 | test "json.test.n_number_Inf" { |
| 944 | err( | 944 | err( |
| 945 | \\[Inf] | 945 | \\[Inf] |
| 946 | ); | 946 | ); |
| 947 | } | 947 | } |
| 948 | 948 | ||
| 949 | test "n_number_invalid+-" { | 949 | test "json.test.n_number_invalid+-" { |
| 950 | err( | 950 | err( |
| 951 | \\[0e+-1] | 951 | \\[0e+-1] |
| 952 | ); | 952 | ); |
| 953 | } | 953 | } |
| 954 | 954 | ||
| 955 | test "n_number_invalid-negative-real" { | 955 | test "json.test.n_number_invalid-negative-real" { |
| 956 | err( | 956 | err( |
| 957 | \\[-123.123foo] | 957 | \\[-123.123foo] |
| 958 | ); | 958 | ); |
| 959 | } | 959 | } |
| 960 | 960 | ||
| 961 | test "n_number_invalid-utf-8-in-bigger-int" { | 961 | test "json.test.n_number_invalid-utf-8-in-bigger-int" { |
| 962 | err( | 962 | err( |
| 963 | \\[123å] | 963 | \\[123å] |
| 964 | ); | 964 | ); |
| 965 | } | 965 | } |
| 966 | 966 | ||
| 967 | test "n_number_invalid-utf-8-in-exponent" { | 967 | test "json.test.n_number_invalid-utf-8-in-exponent" { |
| 968 | err( | 968 | err( |
| 969 | \\[1e1å] | 969 | \\[1e1å] |
| 970 | ); | 970 | ); |
| 971 | } | 971 | } |
| 972 | 972 | ||
| 973 | test "n_number_invalid-utf-8-in-int" { | 973 | test "json.test.n_number_invalid-utf-8-in-int" { |
| 974 | err( | 974 | err( |
| 975 | \\[0å] | 975 | \\[0å] |
| 976 | ); | 976 | ); |
| 977 | } | 977 | } |
| 978 | 978 | ||
| 979 | test "n_number_++" { | 979 | test "json.test.n_number_++" { |
| 980 | err( | 980 | err( |
| 981 | \\[++1234] | 981 | \\[++1234] |
| 982 | ); | 982 | ); |
| 983 | } | 983 | } |
| 984 | 984 | ||
| 985 | test "n_number_minus_infinity" { | 985 | test "json.test.n_number_minus_infinity" { |
| 986 | err( | 986 | err( |
| 987 | \\[-Infinity] | 987 | \\[-Infinity] |
| 988 | ); | 988 | ); |
| 989 | } | 989 | } |
| 990 | 990 | ||
| 991 | test "n_number_minus_sign_with_trailing_garbage" { | 991 | test "json.test.n_number_minus_sign_with_trailing_garbage" { |
| 992 | err( | 992 | err( |
| 993 | \\[-foo] | 993 | \\[-foo] |
| 994 | ); | 994 | ); |
| 995 | } | 995 | } |
| 996 | 996 | ||
| 997 | test "n_number_minus_space_1" { | 997 | test "json.test.n_number_minus_space_1" { |
| 998 | err( | 998 | err( |
| 999 | \\[- 1] | 999 | \\[- 1] |
| 1000 | ); | 1000 | ); |
| 1001 | } | 1001 | } |
| 1002 | 1002 | ||
| 1003 | test "n_number_-NaN" { | 1003 | test "json.test.n_number_-NaN" { |
| 1004 | err( | 1004 | err( |
| 1005 | \\[-NaN] | 1005 | \\[-NaN] |
| 1006 | ); | 1006 | ); |
| 1007 | } | 1007 | } |
| 1008 | 1008 | ||
| 1009 | test "n_number_NaN" { | 1009 | test "json.test.n_number_NaN" { |
| 1010 | err( | 1010 | err( |
| 1011 | \\[NaN] | 1011 | \\[NaN] |
| 1012 | ); | 1012 | ); |
| 1013 | } | 1013 | } |
| 1014 | 1014 | ||
| 1015 | test "n_number_neg_int_starting_with_zero" { | 1015 | test "json.test.n_number_neg_int_starting_with_zero" { |
| 1016 | err( | 1016 | err( |
| 1017 | \\[-012] | 1017 | \\[-012] |
| 1018 | ); | 1018 | ); |
| 1019 | } | 1019 | } |
| 1020 | 1020 | ||
| 1021 | test "n_number_neg_real_without_int_part" { | 1021 | test "json.test.n_number_neg_real_without_int_part" { |
| 1022 | err( | 1022 | err( |
| 1023 | \\[-.123] | 1023 | \\[-.123] |
| 1024 | ); | 1024 | ); |
| 1025 | } | 1025 | } |
| 1026 | 1026 | ||
| 1027 | test "n_number_neg_with_garbage_at_end" { | 1027 | test "json.test.n_number_neg_with_garbage_at_end" { |
| 1028 | err( | 1028 | err( |
| 1029 | \\[-1x] | 1029 | \\[-1x] |
| 1030 | ); | 1030 | ); |
| 1031 | } | 1031 | } |
| 1032 | 1032 | ||
| 1033 | test "n_number_real_garbage_after_e" { | 1033 | test "json.test.n_number_real_garbage_after_e" { |
| 1034 | err( | 1034 | err( |
| 1035 | \\[1ea] | 1035 | \\[1ea] |
| 1036 | ); | 1036 | ); |
| 1037 | } | 1037 | } |
| 1038 | 1038 | ||
| 1039 | test "n_number_real_with_invalid_utf8_after_e" { | 1039 | test "json.test.n_number_real_with_invalid_utf8_after_e" { |
| 1040 | err( | 1040 | err( |
| 1041 | \\[1eå] | 1041 | \\[1eå] |
| 1042 | ); | 1042 | ); |
| 1043 | } | 1043 | } |
| 1044 | 1044 | ||
| 1045 | test "n_number_real_without_fractional_part" { | 1045 | test "json.test.n_number_real_without_fractional_part" { |
| 1046 | err( | 1046 | err( |
| 1047 | \\[1.] | 1047 | \\[1.] |
| 1048 | ); | 1048 | ); |
| 1049 | } | 1049 | } |
| 1050 | 1050 | ||
| 1051 | test "n_number_starting_with_dot" { | 1051 | test "json.test.n_number_starting_with_dot" { |
| 1052 | err( | 1052 | err( |
| 1053 | \\[.123] | 1053 | \\[.123] |
| 1054 | ); | 1054 | ); |
| 1055 | } | 1055 | } |
| 1056 | 1056 | ||
| 1057 | test "n_number_U+FF11_fullwidth_digit_one" { | 1057 | test "json.test.n_number_U+FF11_fullwidth_digit_one" { |
| 1058 | err( | 1058 | err( |
| 1059 | \\[ï¼] | 1059 | \\[ï¼] |
| 1060 | ); | 1060 | ); |
| 1061 | } | 1061 | } |
| 1062 | 1062 | ||
| 1063 | test "n_number_with_alpha_char" { | 1063 | test "json.test.n_number_with_alpha_char" { |
| 1064 | err( | 1064 | err( |
| 1065 | \\[1.8011670033376514H-308] | 1065 | \\[1.8011670033376514H-308] |
| 1066 | ); | 1066 | ); |
| 1067 | } | 1067 | } |
| 1068 | 1068 | ||
| 1069 | test "n_number_with_alpha" { | 1069 | test "json.test.n_number_with_alpha" { |
| 1070 | err( | 1070 | err( |
| 1071 | \\[1.2a-3] | 1071 | \\[1.2a-3] |
| 1072 | ); | 1072 | ); |
| 1073 | } | 1073 | } |
| 1074 | 1074 | ||
| 1075 | test "n_number_with_leading_zero" { | 1075 | test "json.test.n_number_with_leading_zero" { |
| 1076 | err( | 1076 | err( |
| 1077 | \\[012] | 1077 | \\[012] |
| 1078 | ); | 1078 | ); |
| 1079 | } | 1079 | } |
| 1080 | 1080 | ||
| 1081 | test "n_object_bad_value" { | 1081 | test "json.test.n_object_bad_value" { |
| 1082 | err( | 1082 | err( |
| 1083 | \\["x", truth] | 1083 | \\["x", truth] |
| 1084 | ); | 1084 | ); |
| 1085 | } | 1085 | } |
| 1086 | 1086 | ||
| 1087 | test "n_object_bracket_key" { | 1087 | test "json.test.n_object_bracket_key" { |
| 1088 | err( | 1088 | err( |
| 1089 | \\{[: "x"} | 1089 | \\{[: "x"} |
| 1090 | ); | 1090 | ); |
| 1091 | } | 1091 | } |
| 1092 | 1092 | ||
| 1093 | test "n_object_comma_instead_of_colon" { | 1093 | test "json.test.n_object_comma_instead_of_colon" { |
| 1094 | err( | 1094 | err( |
| 1095 | \\{"x", null} | 1095 | \\{"x", null} |
| 1096 | ); | 1096 | ); |
| 1097 | } | 1097 | } |
| 1098 | 1098 | ||
| 1099 | test "n_object_double_colon" { | 1099 | test "json.test.n_object_double_colon" { |
| 1100 | err( | 1100 | err( |
| 1101 | \\{"x"::"b"} | 1101 | \\{"x"::"b"} |
| 1102 | ); | 1102 | ); |
| 1103 | } | 1103 | } |
| 1104 | 1104 | ||
| 1105 | test "n_object_emoji" { | 1105 | test "json.test.n_object_emoji" { |
| 1106 | err( | 1106 | err( |
| 1107 | \\{ð¨ð} | 1107 | \\{ð¨ð} |
| 1108 | ); | 1108 | ); |
| 1109 | } | 1109 | } |
| 1110 | 1110 | ||
| 1111 | test "n_object_garbage_at_end" { | 1111 | test "json.test.n_object_garbage_at_end" { |
| 1112 | err( | 1112 | err( |
| 1113 | \\{"a":"a" 123} | 1113 | \\{"a":"a" 123} |
| 1114 | ); | 1114 | ); |
| 1115 | } | 1115 | } |
| 1116 | 1116 | ||
| 1117 | test "n_object_key_with_single_quotes" { | 1117 | test "json.test.n_object_key_with_single_quotes" { |
| 1118 | err( | 1118 | err( |
| 1119 | \\{key: 'value'} | 1119 | \\{key: 'value'} |
| 1120 | ); | 1120 | ); |
| 1121 | } | 1121 | } |
| 1122 | 1122 | ||
| 1123 | test "n_object_lone_continuation_byte_in_key_and_trailing_comma" { | 1123 | test "json.test.n_object_lone_continuation_byte_in_key_and_trailing_comma" { |
| 1124 | err( | 1124 | err( |
| 1125 | \\{"¹":"0",} | 1125 | \\{"¹":"0",} |
| 1126 | ); | 1126 | ); |
| 1127 | } | 1127 | } |
| 1128 | 1128 | ||
| 1129 | test "n_object_missing_colon" { | 1129 | test "json.test.n_object_missing_colon" { |
| 1130 | err( | 1130 | err( |
| 1131 | \\{"a" b} | 1131 | \\{"a" b} |
| 1132 | ); | 1132 | ); |
| 1133 | } | 1133 | } |
| 1134 | 1134 | ||
| 1135 | test "n_object_missing_key" { | 1135 | test "json.test.n_object_missing_key" { |
| 1136 | err( | 1136 | err( |
| 1137 | \\{:"b"} | 1137 | \\{:"b"} |
| 1138 | ); | 1138 | ); |
| 1139 | } | 1139 | } |
| 1140 | 1140 | ||
| 1141 | test "n_object_missing_semicolon" { | 1141 | test "json.test.n_object_missing_semicolon" { |
| 1142 | err( | 1142 | err( |
| 1143 | \\{"a" "b"} | 1143 | \\{"a" "b"} |
| 1144 | ); | 1144 | ); |
| 1145 | } | 1145 | } |
| 1146 | 1146 | ||
| 1147 | test "n_object_missing_value" { | 1147 | test "json.test.n_object_missing_value" { |
| 1148 | err( | 1148 | err( |
| 1149 | \\{"a": | 1149 | \\{"a": |
| 1150 | ); | 1150 | ); |
| 1151 | } | 1151 | } |
| 1152 | 1152 | ||
| 1153 | test "n_object_no-colon" { | 1153 | test "json.test.n_object_no-colon" { |
| 1154 | err( | 1154 | err( |
| 1155 | \\{"a" | 1155 | \\{"a" |
| 1156 | ); | 1156 | ); |
| 1157 | } | 1157 | } |
| 1158 | 1158 | ||
| 1159 | test "n_object_non_string_key_but_huge_number_instead" { | 1159 | test "json.test.n_object_non_string_key_but_huge_number_instead" { |
| 1160 | err( | 1160 | err( |
| 1161 | \\{9999E9999:1} | 1161 | \\{9999E9999:1} |
| 1162 | ); | 1162 | ); |
| 1163 | } | 1163 | } |
| 1164 | 1164 | ||
| 1165 | test "n_object_non_string_key" { | 1165 | test "json.test.n_object_non_string_key" { |
| 1166 | err( | 1166 | err( |
| 1167 | \\{1:1} | 1167 | \\{1:1} |
| 1168 | ); | 1168 | ); |
| 1169 | } | 1169 | } |
| 1170 | 1170 | ||
| 1171 | test "n_object_repeated_null_null" { | 1171 | test "json.test.n_object_repeated_null_null" { |
| 1172 | err( | 1172 | err( |
| 1173 | \\{null:null,null:null} | 1173 | \\{null:null,null:null} |
| 1174 | ); | 1174 | ); |
| 1175 | } | 1175 | } |
| 1176 | 1176 | ||
| 1177 | test "n_object_several_trailing_commas" { | 1177 | test "json.test.n_object_several_trailing_commas" { |
| 1178 | err( | 1178 | err( |
| 1179 | \\{"id":0,,,,,} | 1179 | \\{"id":0,,,,,} |
| 1180 | ); | 1180 | ); |
| 1181 | } | 1181 | } |
| 1182 | 1182 | ||
| 1183 | test "n_object_single_quote" { | 1183 | test "json.test.n_object_single_quote" { |
| 1184 | err( | 1184 | err( |
| 1185 | \\{'a':0} | 1185 | \\{'a':0} |
| 1186 | ); | 1186 | ); |
| 1187 | } | 1187 | } |
| 1188 | 1188 | ||
| 1189 | test "n_object_trailing_comma" { | 1189 | test "json.test.n_object_trailing_comma" { |
| 1190 | err( | 1190 | err( |
| 1191 | \\{"id":0,} | 1191 | \\{"id":0,} |
| 1192 | ); | 1192 | ); |
| 1193 | } | 1193 | } |
| 1194 | 1194 | ||
| 1195 | test "n_object_trailing_comment" { | 1195 | test "json.test.n_object_trailing_comment" { |
| 1196 | err( | 1196 | err( |
| 1197 | \\{"a":"b"}/**/ | 1197 | \\{"a":"b"}/**/ |
| 1198 | ); | 1198 | ); |
| 1199 | } | 1199 | } |
| 1200 | 1200 | ||
| 1201 | test "n_object_trailing_comment_open" { | 1201 | test "json.test.n_object_trailing_comment_open" { |
| 1202 | err( | 1202 | err( |
| 1203 | \\{"a":"b"}/**// | 1203 | \\{"a":"b"}/**// |
| 1204 | ); | 1204 | ); |
| 1205 | } | 1205 | } |
| 1206 | 1206 | ||
| 1207 | test "n_object_trailing_comment_slash_open_incomplete" { | 1207 | test "json.test.n_object_trailing_comment_slash_open_incomplete" { |
| 1208 | err( | 1208 | err( |
| 1209 | \\{"a":"b"}/ | 1209 | \\{"a":"b"}/ |
| 1210 | ); | 1210 | ); |
| 1211 | } | 1211 | } |
| 1212 | 1212 | ||
| 1213 | test "n_object_trailing_comment_slash_open" { | 1213 | test "json.test.n_object_trailing_comment_slash_open" { |
| 1214 | err( | 1214 | err( |
| 1215 | \\{"a":"b"}// | 1215 | \\{"a":"b"}// |
| 1216 | ); | 1216 | ); |
| 1217 | } | 1217 | } |
| 1218 | 1218 | ||
| 1219 | test "n_object_two_commas_in_a_row" { | 1219 | test "json.test.n_object_two_commas_in_a_row" { |
| 1220 | err( | 1220 | err( |
| 1221 | \\{"a":"b",,"c":"d"} | 1221 | \\{"a":"b",,"c":"d"} |
| 1222 | ); | 1222 | ); |
| 1223 | } | 1223 | } |
| 1224 | 1224 | ||
| 1225 | test "n_object_unquoted_key" { | 1225 | test "json.test.n_object_unquoted_key" { |
| 1226 | err( | 1226 | err( |
| 1227 | \\{a: "b"} | 1227 | \\{a: "b"} |
| 1228 | ); | 1228 | ); |
| 1229 | } | 1229 | } |
| 1230 | 1230 | ||
| 1231 | test "n_object_unterminated-value" { | 1231 | test "json.test.n_object_unterminated-value" { |
| 1232 | err( | 1232 | err( |
| 1233 | \\{"a":"a | 1233 | \\{"a":"a |
| 1234 | ); | 1234 | ); |
| 1235 | } | 1235 | } |
| 1236 | 1236 | ||
| 1237 | test "n_object_with_single_string" { | 1237 | test "json.test.n_object_with_single_string" { |
| 1238 | err( | 1238 | err( |
| 1239 | \\{ "foo" : "bar", "a" } | 1239 | \\{ "foo" : "bar", "a" } |
| 1240 | ); | 1240 | ); |
| 1241 | } | 1241 | } |
| 1242 | 1242 | ||
| 1243 | test "n_object_with_trailing_garbage" { | 1243 | test "json.test.n_object_with_trailing_garbage" { |
| 1244 | err( | 1244 | err( |
| 1245 | \\{"a":"b"}# | 1245 | \\{"a":"b"}# |
| 1246 | ); | 1246 | ); |
| 1247 | } | 1247 | } |
| 1248 | 1248 | ||
| 1249 | test "n_single_space" { | 1249 | test "json.test.n_single_space" { |
| 1250 | err(" "); | 1250 | err(" "); |
| 1251 | } | 1251 | } |
| 1252 | 1252 | ||
| 1253 | test "n_string_1_surrogate_then_escape" { | 1253 | test "json.test.n_string_1_surrogate_then_escape" { |
| 1254 | err( | 1254 | err( |
| 1255 | \\["\uD800\"] | 1255 | \\["\uD800\"] |
| 1256 | ); | 1256 | ); |
| 1257 | } | 1257 | } |
| 1258 | 1258 | ||
| 1259 | test "n_string_1_surrogate_then_escape_u1" { | 1259 | test "json.test.n_string_1_surrogate_then_escape_u1" { |
| 1260 | err( | 1260 | err( |
| 1261 | \\["\uD800\u1"] | 1261 | \\["\uD800\u1"] |
| 1262 | ); | 1262 | ); |
| 1263 | } | 1263 | } |
| 1264 | 1264 | ||
| 1265 | test "n_string_1_surrogate_then_escape_u1x" { | 1265 | test "json.test.n_string_1_surrogate_then_escape_u1x" { |
| 1266 | err( | 1266 | err( |
| 1267 | \\["\uD800\u1x"] | 1267 | \\["\uD800\u1x"] |
| 1268 | ); | 1268 | ); |
| 1269 | } | 1269 | } |
| 1270 | 1270 | ||
| 1271 | test "n_string_1_surrogate_then_escape_u" { | 1271 | test "json.test.n_string_1_surrogate_then_escape_u" { |
| 1272 | err( | 1272 | err( |
| 1273 | \\["\uD800\u"] | 1273 | \\["\uD800\u"] |
| 1274 | ); | 1274 | ); |
| 1275 | } | 1275 | } |
| 1276 | 1276 | ||
| 1277 | test "n_string_accentuated_char_no_quotes" { | 1277 | test "json.test.n_string_accentuated_char_no_quotes" { |
| 1278 | err( | 1278 | err( |
| 1279 | \\[é] | 1279 | \\[é] |
| 1280 | ); | 1280 | ); |
| 1281 | } | 1281 | } |
| 1282 | 1282 | ||
| 1283 | test "n_string_backslash_00" { | 1283 | test "json.test.n_string_backslash_00" { |
| 1284 | err("[\"\x00\"]"); | 1284 | err("[\"\x00\"]"); |
| 1285 | } | 1285 | } |
| 1286 | 1286 | ||
| 1287 | test "n_string_escaped_backslash_bad" { | 1287 | test "json.test.n_string_escaped_backslash_bad" { |
| 1288 | err( | 1288 | err( |
| 1289 | \\["\\\"] | 1289 | \\["\\\"] |
| 1290 | ); | 1290 | ); |
| 1291 | } | 1291 | } |
| 1292 | 1292 | ||
| 1293 | test "n_string_escaped_ctrl_char_tab" { | 1293 | test "json.test.n_string_escaped_ctrl_char_tab" { |
| 1294 | err("\x5b\x22\x5c\x09\x22\x5d"); | 1294 | err("\x5b\x22\x5c\x09\x22\x5d"); |
| 1295 | } | 1295 | } |
| 1296 | 1296 | ||
| 1297 | test "n_string_escaped_emoji" { | 1297 | test "json.test.n_string_escaped_emoji" { |
| 1298 | err("[\"\x5c\xc3\xb0\xc2\x9f\xc2\x8c\xc2\x80\"]"); | 1298 | err("[\"\x5c\xc3\xb0\xc2\x9f\xc2\x8c\xc2\x80\"]"); |
| 1299 | } | 1299 | } |
| 1300 | 1300 | ||
| 1301 | test "n_string_escape_x" { | 1301 | test "json.test.n_string_escape_x" { |
| 1302 | err( | 1302 | err( |
| 1303 | \\["\x00"] | 1303 | \\["\x00"] |
| 1304 | ); | 1304 | ); |
| 1305 | } | 1305 | } |
| 1306 | 1306 | ||
| 1307 | test "n_string_incomplete_escaped_character" { | 1307 | test "json.test.n_string_incomplete_escaped_character" { |
| 1308 | err( | 1308 | err( |
| 1309 | \\["\u00A"] | 1309 | \\["\u00A"] |
| 1310 | ); | 1310 | ); |
| 1311 | } | 1311 | } |
| 1312 | 1312 | ||
| 1313 | test "n_string_incomplete_escape" { | 1313 | test "json.test.n_string_incomplete_escape" { |
| 1314 | err( | 1314 | err( |
| 1315 | \\["\"] | 1315 | \\["\"] |
| 1316 | ); | 1316 | ); |
| 1317 | } | 1317 | } |
| 1318 | 1318 | ||
| 1319 | test "n_string_incomplete_surrogate_escape_invalid" { | 1319 | test "json.test.n_string_incomplete_surrogate_escape_invalid" { |
| 1320 | err( | 1320 | err( |
| 1321 | \\["\uD800\uD800\x"] | 1321 | \\["\uD800\uD800\x"] |
| 1322 | ); | 1322 | ); |
| 1323 | } | 1323 | } |
| 1324 | 1324 | ||
| 1325 | test "n_string_incomplete_surrogate" { | 1325 | test "json.test.n_string_incomplete_surrogate" { |
| 1326 | err( | 1326 | err( |
| 1327 | \\["\uD834\uDd"] | 1327 | \\["\uD834\uDd"] |
| 1328 | ); | 1328 | ); |
| 1329 | } | 1329 | } |
| 1330 | 1330 | ||
| 1331 | test "n_string_invalid_backslash_esc" { | 1331 | test "json.test.n_string_invalid_backslash_esc" { |
| 1332 | err( | 1332 | err( |
| 1333 | \\["\a"] | 1333 | \\["\a"] |
| 1334 | ); | 1334 | ); |
| 1335 | } | 1335 | } |
| 1336 | 1336 | ||
| 1337 | test "n_string_invalid_unicode_escape" { | 1337 | test "json.test.n_string_invalid_unicode_escape" { |
| 1338 | err( | 1338 | err( |
| 1339 | \\["\uqqqq"] | 1339 | \\["\uqqqq"] |
| 1340 | ); | 1340 | ); |
| 1341 | } | 1341 | } |
| 1342 | 1342 | ||
| 1343 | test "n_string_invalid_utf8_after_escape" { | 1343 | test "json.test.n_string_invalid_utf8_after_escape" { |
| 1344 | err("[\"\\\x75\xc3\xa5\"]"); | 1344 | err("[\"\\\x75\xc3\xa5\"]"); |
| 1345 | } | 1345 | } |
| 1346 | 1346 | ||
| 1347 | test "n_string_invalid-utf-8-in-escape" { | 1347 | test "json.test.n_string_invalid-utf-8-in-escape" { |
| 1348 | err( | 1348 | err( |
| 1349 | \\["\uå"] | 1349 | \\["\uå"] |
| 1350 | ); | 1350 | ); |
| 1351 | } | 1351 | } |
| 1352 | 1352 | ||
| 1353 | test "n_string_leading_uescaped_thinspace" { | 1353 | test "json.test.n_string_leading_uescaped_thinspace" { |
| 1354 | err( | 1354 | err( |
| 1355 | \\[\u0020"asd"] | 1355 | \\[\u0020"asd"] |
| 1356 | ); | 1356 | ); |
| 1357 | } | 1357 | } |
| 1358 | 1358 | ||
| 1359 | test "n_string_no_quotes_with_bad_escape" { | 1359 | test "json.test.n_string_no_quotes_with_bad_escape" { |
| 1360 | err( | 1360 | err( |
| 1361 | \\[\n] | 1361 | \\[\n] |
| 1362 | ); | 1362 | ); |
| 1363 | } | 1363 | } |
| 1364 | 1364 | ||
| 1365 | test "n_string_single_doublequote" { | 1365 | test "json.test.n_string_single_doublequote" { |
| 1366 | err( | 1366 | err( |
| 1367 | \\" | 1367 | \\" |
| 1368 | ); | 1368 | ); |
| 1369 | } | 1369 | } |
| 1370 | 1370 | ||
| 1371 | test "n_string_single_quote" { | 1371 | test "json.test.n_string_single_quote" { |
| 1372 | err( | 1372 | err( |
| 1373 | \\['single quote'] | 1373 | \\['single quote'] |
| 1374 | ); | 1374 | ); |
| 1375 | } | 1375 | } |
| 1376 | 1376 | ||
| 1377 | test "n_string_single_string_no_double_quotes" { | 1377 | test "json.test.n_string_single_string_no_double_quotes" { |
| 1378 | err( | 1378 | err( |
| 1379 | \\abc | 1379 | \\abc |
| 1380 | ); | 1380 | ); |
| 1381 | } | 1381 | } |
| 1382 | 1382 | ||
| 1383 | test "n_string_start_escape_unclosed" { | 1383 | test "json.test.n_string_start_escape_unclosed" { |
| 1384 | err( | 1384 | err( |
| 1385 | \\["\ | 1385 | \\["\ |
| 1386 | ); | 1386 | ); |
| 1387 | } | 1387 | } |
| 1388 | 1388 | ||
| 1389 | test "n_string_unescaped_crtl_char" { | 1389 | test "json.test.n_string_unescaped_crtl_char" { |
| 1390 | err("[\"a\x00a\"]"); | 1390 | err("[\"a\x00a\"]"); |
| 1391 | } | 1391 | } |
| 1392 | 1392 | ||
| 1393 | test "n_string_unescaped_newline" { | 1393 | test "json.test.n_string_unescaped_newline" { |
| 1394 | err( | 1394 | err( |
| 1395 | \\["new | 1395 | \\["new |
| 1396 | \\line"] | 1396 | \\line"] |
| 1397 | ); | 1397 | ); |
| 1398 | } | 1398 | } |
| 1399 | 1399 | ||
| 1400 | test "n_string_unescaped_tab" { | 1400 | test "json.test.n_string_unescaped_tab" { |
| 1401 | err("[\"\t\"]"); | 1401 | err("[\"\t\"]"); |
| 1402 | } | 1402 | } |
| 1403 | 1403 | ||
| 1404 | test "n_string_unicode_CapitalU" { | 1404 | test "json.test.n_string_unicode_CapitalU" { |
| 1405 | err( | 1405 | err( |
| 1406 | \\"\UA66D" | 1406 | \\"\UA66D" |
| 1407 | ); | 1407 | ); |
| 1408 | } | 1408 | } |
| 1409 | 1409 | ||
| 1410 | test "n_string_with_trailing_garbage" { | 1410 | test "json.test.n_string_with_trailing_garbage" { |
| 1411 | err( | 1411 | err( |
| 1412 | \\""x | 1412 | \\""x |
| 1413 | ); | 1413 | ); |
| 1414 | } | 1414 | } |
| 1415 | 1415 | ||
| 1416 | test "n_structure_100000_opening_arrays" { | 1416 | test "json.test.n_structure_100000_opening_arrays" { |
| 1417 | err("[" ** 100000); | 1417 | err("[" ** 100000); |
| 1418 | } | 1418 | } |
| 1419 | 1419 | ||
| 1420 | test "n_structure_angle_bracket_." { | 1420 | test "json.test.n_structure_angle_bracket_." { |
| 1421 | err( | 1421 | err( |
| 1422 | \\<.> | 1422 | \\<.> |
| 1423 | ); | 1423 | ); |
| 1424 | } | 1424 | } |
| 1425 | 1425 | ||
| 1426 | test "n_structure_angle_bracket_null" { | 1426 | test "json.test.n_structure_angle_bracket_null" { |
| 1427 | err( | 1427 | err( |
| 1428 | \\[<null>] | 1428 | \\[<null>] |
| 1429 | ); | 1429 | ); |
| 1430 | } | 1430 | } |
| 1431 | 1431 | ||
| 1432 | test "n_structure_array_trailing_garbage" { | 1432 | test "json.test.n_structure_array_trailing_garbage" { |
| 1433 | err( | 1433 | err( |
| 1434 | \\[1]x | 1434 | \\[1]x |
| 1435 | ); | 1435 | ); |
| 1436 | } | 1436 | } |
| 1437 | 1437 | ||
| 1438 | test "n_structure_array_with_extra_array_close" { | 1438 | test "json.test.n_structure_array_with_extra_array_close" { |
| 1439 | err( | 1439 | err( |
| 1440 | \\[1]] | 1440 | \\[1]] |
| 1441 | ); | 1441 | ); |
| 1442 | } | 1442 | } |
| 1443 | 1443 | ||
| 1444 | test "n_structure_array_with_unclosed_string" { | 1444 | test "json.test.n_structure_array_with_unclosed_string" { |
| 1445 | err( | 1445 | err( |
| 1446 | \\["asd] | 1446 | \\["asd] |
| 1447 | ); | 1447 | ); |
| 1448 | } | 1448 | } |
| 1449 | 1449 | ||
| 1450 | test "n_structure_ascii-unicode-identifier" { | 1450 | test "json.test.n_structure_ascii-unicode-identifier" { |
| 1451 | err( | 1451 | err( |
| 1452 | \\aå | 1452 | \\aå |
| 1453 | ); | 1453 | ); |
| 1454 | } | 1454 | } |
| 1455 | 1455 | ||
| 1456 | test "n_structure_capitalized_True" { | 1456 | test "json.test.n_structure_capitalized_True" { |
| 1457 | err( | 1457 | err( |
| 1458 | \\[True] | 1458 | \\[True] |
| 1459 | ); | 1459 | ); |
| 1460 | } | 1460 | } |
| 1461 | 1461 | ||
| 1462 | test "n_structure_close_unopened_array" { | 1462 | test "json.test.n_structure_close_unopened_array" { |
| 1463 | err( | 1463 | err( |
| 1464 | \\1] | 1464 | \\1] |
| 1465 | ); | 1465 | ); |
| 1466 | } | 1466 | } |
| 1467 | 1467 | ||
| 1468 | test "n_structure_comma_instead_of_closing_brace" { | 1468 | test "json.test.n_structure_comma_instead_of_closing_brace" { |
| 1469 | err( | 1469 | err( |
| 1470 | \\{"x": true, | 1470 | \\{"x": true, |
| 1471 | ); | 1471 | ); |
| 1472 | } | 1472 | } |
| 1473 | 1473 | ||
| 1474 | test "n_structure_double_array" { | 1474 | test "json.test.n_structure_double_array" { |
| 1475 | err( | 1475 | err( |
| 1476 | \\[][] | 1476 | \\[][] |
| 1477 | ); | 1477 | ); |
| 1478 | } | 1478 | } |
| 1479 | 1479 | ||
| 1480 | test "n_structure_end_array" { | 1480 | test "json.test.n_structure_end_array" { |
| 1481 | err( | 1481 | err( |
| 1482 | \\] | 1482 | \\] |
| 1483 | ); | 1483 | ); |
| 1484 | } | 1484 | } |
| 1485 | 1485 | ||
| 1486 | test "n_structure_incomplete_UTF8_BOM" { | 1486 | test "json.test.n_structure_incomplete_UTF8_BOM" { |
| 1487 | err( | 1487 | err( |
| 1488 | \\ï»{} | 1488 | \\ï»{} |
| 1489 | ); | 1489 | ); |
| 1490 | } | 1490 | } |
| 1491 | 1491 | ||
| 1492 | test "n_structure_lone-invalid-utf-8" { | 1492 | test "json.test.n_structure_lone-invalid-utf-8" { |
| 1493 | err( | 1493 | err( |
| 1494 | \\å | 1494 | \\å |
| 1495 | ); | 1495 | ); |
| 1496 | } | 1496 | } |
| 1497 | 1497 | ||
| 1498 | test "n_structure_lone-open-bracket" { | 1498 | test "json.test.n_structure_lone-open-bracket" { |
| 1499 | err( | 1499 | err( |
| 1500 | \\[ | 1500 | \\[ |
| 1501 | ); | 1501 | ); |
| 1502 | } | 1502 | } |
| 1503 | 1503 | ||
| 1504 | test "n_structure_no_data" { | 1504 | test "json.test.n_structure_no_data" { |
| 1505 | err( | 1505 | err( |
| 1506 | \\ | 1506 | \\ |
| 1507 | ); | 1507 | ); |
| 1508 | } | 1508 | } |
| 1509 | 1509 | ||
| 1510 | test "n_structure_null-byte-outside-string" { | 1510 | test "json.test.n_structure_null-byte-outside-string" { |
| 1511 | err("[\x00]"); | 1511 | err("[\x00]"); |
| 1512 | } | 1512 | } |
| 1513 | 1513 | ||
| 1514 | test "n_structure_number_with_trailing_garbage" { | 1514 | test "json.test.n_structure_number_with_trailing_garbage" { |
| 1515 | err( | 1515 | err( |
| 1516 | \\2@ | 1516 | \\2@ |
| 1517 | ); | 1517 | ); |
| 1518 | } | 1518 | } |
| 1519 | 1519 | ||
| 1520 | test "n_structure_object_followed_by_closing_object" { | 1520 | test "json.test.n_structure_object_followed_by_closing_object" { |
| 1521 | err( | 1521 | err( |
| 1522 | \\{}} | 1522 | \\{}} |
| 1523 | ); | 1523 | ); |
| 1524 | } | 1524 | } |
| 1525 | 1525 | ||
| 1526 | test "n_structure_object_unclosed_no_value" { | 1526 | test "json.test.n_structure_object_unclosed_no_value" { |
| 1527 | err( | 1527 | err( |
| 1528 | \\{"": | 1528 | \\{"": |
| 1529 | ); | 1529 | ); |
| 1530 | } | 1530 | } |
| 1531 | 1531 | ||
| 1532 | test "n_structure_object_with_comment" { | 1532 | test "json.test.n_structure_object_with_comment" { |
| 1533 | err( | 1533 | err( |
| 1534 | \\{"a":/*comment*/"b"} | 1534 | \\{"a":/*comment*/"b"} |
| 1535 | ); | 1535 | ); |
| 1536 | } | 1536 | } |
| 1537 | 1537 | ||
| 1538 | test "n_structure_object_with_trailing_garbage" { | 1538 | test "json.test.n_structure_object_with_trailing_garbage" { |
| 1539 | err( | 1539 | err( |
| 1540 | \\{"a": true} "x" | 1540 | \\{"a": true} "x" |
| 1541 | ); | 1541 | ); |
| 1542 | } | 1542 | } |
| 1543 | 1543 | ||
| 1544 | test "n_structure_open_array_apostrophe" { | 1544 | test "json.test.n_structure_open_array_apostrophe" { |
| 1545 | err( | 1545 | err( |
| 1546 | \\[' | 1546 | \\[' |
| 1547 | ); | 1547 | ); |
| 1548 | } | 1548 | } |
| 1549 | 1549 | ||
| 1550 | test "n_structure_open_array_comma" { | 1550 | test "json.test.n_structure_open_array_comma" { |
| 1551 | err( | 1551 | err( |
| 1552 | \\[, | 1552 | \\[, |
| 1553 | ); | 1553 | ); |
| 1554 | } | 1554 | } |
| 1555 | 1555 | ||
| 1556 | test "n_structure_open_array_object" { | 1556 | test "json.test.n_structure_open_array_object" { |
| 1557 | err("[{\"\":" ** 50000); | 1557 | err("[{\"\":" ** 50000); |
| 1558 | } | 1558 | } |
| 1559 | 1559 | ||
| 1560 | test "n_structure_open_array_open_object" { | 1560 | test "json.test.n_structure_open_array_open_object" { |
| 1561 | err( | 1561 | err( |
| 1562 | \\[{ | 1562 | \\[{ |
| 1563 | ); | 1563 | ); |
| 1564 | } | 1564 | } |
| 1565 | 1565 | ||
| 1566 | test "n_structure_open_array_open_string" { | 1566 | test "json.test.n_structure_open_array_open_string" { |
| 1567 | err( | 1567 | err( |
| 1568 | \\["a | 1568 | \\["a |
| 1569 | ); | 1569 | ); |
| 1570 | } | 1570 | } |
| 1571 | 1571 | ||
| 1572 | test "n_structure_open_array_string" { | 1572 | test "json.test.n_structure_open_array_string" { |
| 1573 | err( | 1573 | err( |
| 1574 | \\["a" | 1574 | \\["a" |
| 1575 | ); | 1575 | ); |
| 1576 | } | 1576 | } |
| 1577 | 1577 | ||
| 1578 | test "n_structure_open_object_close_array" { | 1578 | test "json.test.n_structure_open_object_close_array" { |
| 1579 | err( | 1579 | err( |
| 1580 | \\{] | 1580 | \\{] |
| 1581 | ); | 1581 | ); |
| 1582 | } | 1582 | } |
| 1583 | 1583 | ||
| 1584 | test "n_structure_open_object_comma" { | 1584 | test "json.test.n_structure_open_object_comma" { |
| 1585 | err( | 1585 | err( |
| 1586 | \\{, | 1586 | \\{, |
| 1587 | ); | 1587 | ); |
| 1588 | } | 1588 | } |
| 1589 | 1589 | ||
| 1590 | test "n_structure_open_object" { | 1590 | test "json.test.n_structure_open_object" { |
| 1591 | err( | 1591 | err( |
| 1592 | \\{ | 1592 | \\{ |
| 1593 | ); | 1593 | ); |
| 1594 | } | 1594 | } |
| 1595 | 1595 | ||
| 1596 | test "n_structure_open_object_open_array" { | 1596 | test "json.test.n_structure_open_object_open_array" { |
| 1597 | err( | 1597 | err( |
| 1598 | \\{[ | 1598 | \\{[ |
| 1599 | ); | 1599 | ); |
| 1600 | } | 1600 | } |
| 1601 | 1601 | ||
| 1602 | test "n_structure_open_object_open_string" { | 1602 | test "json.test.n_structure_open_object_open_string" { |
| 1603 | err( | 1603 | err( |
| 1604 | \\{"a | 1604 | \\{"a |
| 1605 | ); | 1605 | ); |
| 1606 | } | 1606 | } |
| 1607 | 1607 | ||
| 1608 | test "n_structure_open_object_string_with_apostrophes" { | 1608 | test "json.test.n_structure_open_object_string_with_apostrophes" { |
| 1609 | err( | 1609 | err( |
| 1610 | \\{'a' | 1610 | \\{'a' |
| 1611 | ); | 1611 | ); |
| 1612 | } | 1612 | } |
| 1613 | 1613 | ||
| 1614 | test "n_structure_open_open" { | 1614 | test "json.test.n_structure_open_open" { |
| 1615 | err( | 1615 | err( |
| 1616 | \\["\{["\{["\{["\{ | 1616 | \\["\{["\{["\{["\{ |
| 1617 | ); | 1617 | ); |
| 1618 | } | 1618 | } |
| 1619 | 1619 | ||
| 1620 | test "n_structure_single_eacute" { | 1620 | test "json.test.n_structure_single_eacute" { |
| 1621 | err( | 1621 | err( |
| 1622 | \\é | 1622 | \\é |
| 1623 | ); | 1623 | ); |
| 1624 | } | 1624 | } |
| 1625 | 1625 | ||
| 1626 | test "n_structure_single_star" { | 1626 | test "json.test.n_structure_single_star" { |
| 1627 | err( | 1627 | err( |
| 1628 | \\* | 1628 | \\* |
| 1629 | ); | 1629 | ); |
| 1630 | } | 1630 | } |
| 1631 | 1631 | ||
| 1632 | test "n_structure_trailing_#" { | 1632 | test "json.test.n_structure_trailing_#" { |
| 1633 | err( | 1633 | err( |
| 1634 | \\{"a":"b"}#{} | 1634 | \\{"a":"b"}#{} |
| 1635 | ); | 1635 | ); |
| 1636 | } | 1636 | } |
| 1637 | 1637 | ||
| 1638 | test "n_structure_U+2060_word_joined" { | 1638 | test "json.test.n_structure_U+2060_word_joined" { |
| 1639 | err( | 1639 | err( |
| 1640 | \\[â ] | 1640 | \\[â ] |
| 1641 | ); | 1641 | ); |
| 1642 | } | 1642 | } |
| 1643 | 1643 | ||
| 1644 | test "n_structure_uescaped_LF_before_string" { | 1644 | test "json.test.n_structure_uescaped_LF_before_string" { |
| 1645 | err( | 1645 | err( |
| 1646 | \\[\u000A""] | 1646 | \\[\u000A""] |
| 1647 | ); | 1647 | ); |
| 1648 | } | 1648 | } |
| 1649 | 1649 | ||
| 1650 | test "n_structure_unclosed_array" { | 1650 | test "json.test.n_structure_unclosed_array" { |
| 1651 | err( | 1651 | err( |
| 1652 | \\[1 | 1652 | \\[1 |
| 1653 | ); | 1653 | ); |
| 1654 | } | 1654 | } |
| 1655 | 1655 | ||
| 1656 | test "n_structure_unclosed_array_partial_null" { | 1656 | test "json.test.n_structure_unclosed_array_partial_null" { |
| 1657 | err( | 1657 | err( |
| 1658 | \\[ false, nul | 1658 | \\[ false, nul |
| 1659 | ); | 1659 | ); |
| 1660 | } | 1660 | } |
| 1661 | 1661 | ||
| 1662 | test "n_structure_unclosed_array_unfinished_false" { | 1662 | test "json.test.n_structure_unclosed_array_unfinished_false" { |
| 1663 | err( | 1663 | err( |
| 1664 | \\[ true, fals | 1664 | \\[ true, fals |
| 1665 | ); | 1665 | ); |
| 1666 | } | 1666 | } |
| 1667 | 1667 | ||
| 1668 | test "n_structure_unclosed_array_unfinished_true" { | 1668 | test "json.test.n_structure_unclosed_array_unfinished_true" { |
| 1669 | err( | 1669 | err( |
| 1670 | \\[ false, tru | 1670 | \\[ false, tru |
| 1671 | ); | 1671 | ); |
| 1672 | } | 1672 | } |
| 1673 | 1673 | ||
| 1674 | test "n_structure_unclosed_object" { | 1674 | test "json.test.n_structure_unclosed_object" { |
| 1675 | err( | 1675 | err( |
| 1676 | \\{"asd":"asd" | 1676 | \\{"asd":"asd" |
| 1677 | ); | 1677 | ); |
| 1678 | } | 1678 | } |
| 1679 | 1679 | ||
| 1680 | test "n_structure_unicode-identifier" { | 1680 | test "json.test.n_structure_unicode-identifier" { |
| 1681 | err( | 1681 | err( |
| 1682 | \\Ã¥ | 1682 | \\Ã¥ |
| 1683 | ); | 1683 | ); |
| 1684 | } | 1684 | } |
| 1685 | 1685 | ||
| 1686 | test "n_structure_UTF8_BOM_no_data" { | 1686 | test "json.test.n_structure_UTF8_BOM_no_data" { |
| 1687 | err( | 1687 | err( |
| 1688 | \\ | 1688 | \\ |
| 1689 | ); | 1689 | ); |
| 1690 | } | 1690 | } |
| 1691 | 1691 | ||
| 1692 | test "n_structure_whitespace_formfeed" { | 1692 | test "json.test.n_structure_whitespace_formfeed" { |
| 1693 | err("[\x0c]"); | 1693 | err("[\x0c]"); |
| 1694 | } | 1694 | } |
| 1695 | 1695 | ||
| 1696 | test "n_structure_whitespace_U+2060_word_joiner" { | 1696 | test "json.test.n_structure_whitespace_U+2060_word_joiner" { |
| 1697 | err( | 1697 | err( |
| 1698 | \\[â ] | 1698 | \\[â ] |
| 1699 | ); | 1699 | ); |
| ... | @@ -1701,203 +1701,203 @@ test "n_structure_whitespace_U+2060_word_joiner" { | ... | @@ -1701,203 +1701,203 @@ test "n_structure_whitespace_U+2060_word_joiner" { |
| 1701 | 1701 | ||
| 1702 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 1702 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1703 | 1703 | ||
| 1704 | test "i_number_double_huge_neg_exp" { | 1704 | test "json.test.i_number_double_huge_neg_exp" { |
| 1705 | any( | 1705 | any( |
| 1706 | \\[123.456e-789] | 1706 | \\[123.456e-789] |
| 1707 | ); | 1707 | ); |
| 1708 | } | 1708 | } |
| 1709 | 1709 | ||
| 1710 | test "i_number_huge_exp" { | 1710 | test "json.test.i_number_huge_exp" { |
| 1711 | any( | 1711 | any( |
| 1712 | \\[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] | 1712 | \\[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] |
| 1713 | ); | 1713 | ); |
| 1714 | } | 1714 | } |
| 1715 | 1715 | ||
| 1716 | test "i_number_neg_int_huge_exp" { | 1716 | test "json.test.i_number_neg_int_huge_exp" { |
| 1717 | any( | 1717 | any( |
| 1718 | \\[-1e+9999] | 1718 | \\[-1e+9999] |
| 1719 | ); | 1719 | ); |
| 1720 | } | 1720 | } |
| 1721 | 1721 | ||
| 1722 | test "i_number_pos_double_huge_exp" { | 1722 | test "json.test.i_number_pos_double_huge_exp" { |
| 1723 | any( | 1723 | any( |
| 1724 | \\[1.5e+9999] | 1724 | \\[1.5e+9999] |
| 1725 | ); | 1725 | ); |
| 1726 | } | 1726 | } |
| 1727 | 1727 | ||
| 1728 | test "i_number_real_neg_overflow" { | 1728 | test "json.test.i_number_real_neg_overflow" { |
| 1729 | any( | 1729 | any( |
| 1730 | \\[-123123e100000] | 1730 | \\[-123123e100000] |
| 1731 | ); | 1731 | ); |
| 1732 | } | 1732 | } |
| 1733 | 1733 | ||
| 1734 | test "i_number_real_pos_overflow" { | 1734 | test "json.test.i_number_real_pos_overflow" { |
| 1735 | any( | 1735 | any( |
| 1736 | \\[123123e100000] | 1736 | \\[123123e100000] |
| 1737 | ); | 1737 | ); |
| 1738 | } | 1738 | } |
| 1739 | 1739 | ||
| 1740 | test "i_number_real_underflow" { | 1740 | test "json.test.i_number_real_underflow" { |
| 1741 | any( | 1741 | any( |
| 1742 | \\[123e-10000000] | 1742 | \\[123e-10000000] |
| 1743 | ); | 1743 | ); |
| 1744 | } | 1744 | } |
| 1745 | 1745 | ||
| 1746 | test "i_number_too_big_neg_int" { | 1746 | test "json.test.i_number_too_big_neg_int" { |
| 1747 | any( | 1747 | any( |
| 1748 | \\[-123123123123123123123123123123] | 1748 | \\[-123123123123123123123123123123] |
| 1749 | ); | 1749 | ); |
| 1750 | } | 1750 | } |
| 1751 | 1751 | ||
| 1752 | test "i_number_too_big_pos_int" { | 1752 | test "json.test.i_number_too_big_pos_int" { |
| 1753 | any( | 1753 | any( |
| 1754 | \\[100000000000000000000] | 1754 | \\[100000000000000000000] |
| 1755 | ); | 1755 | ); |
| 1756 | } | 1756 | } |
| 1757 | 1757 | ||
| 1758 | test "i_number_very_big_negative_int" { | 1758 | test "json.test.i_number_very_big_negative_int" { |
| 1759 | any( | 1759 | any( |
| 1760 | \\[-237462374673276894279832749832423479823246327846] | 1760 | \\[-237462374673276894279832749832423479823246327846] |
| 1761 | ); | 1761 | ); |
| 1762 | } | 1762 | } |
| 1763 | 1763 | ||
| 1764 | test "i_object_key_lone_2nd_surrogate" { | 1764 | test "json.test.i_object_key_lone_2nd_surrogate" { |
| 1765 | any( | 1765 | any( |
| 1766 | \\{"\uDFAA":0} | 1766 | \\{"\uDFAA":0} |
| 1767 | ); | 1767 | ); |
| 1768 | } | 1768 | } |
| 1769 | 1769 | ||
| 1770 | test "i_string_1st_surrogate_but_2nd_missing" { | 1770 | test "json.test.i_string_1st_surrogate_but_2nd_missing" { |
| 1771 | any( | 1771 | any( |
| 1772 | \\["\uDADA"] | 1772 | \\["\uDADA"] |
| 1773 | ); | 1773 | ); |
| 1774 | } | 1774 | } |
| 1775 | 1775 | ||
| 1776 | test "i_string_1st_valid_surrogate_2nd_invalid" { | 1776 | test "json.test.i_string_1st_valid_surrogate_2nd_invalid" { |
| 1777 | any( | 1777 | any( |
| 1778 | \\["\uD888\u1234"] | 1778 | \\["\uD888\u1234"] |
| 1779 | ); | 1779 | ); |
| 1780 | } | 1780 | } |
| 1781 | 1781 | ||
| 1782 | test "i_string_incomplete_surrogate_and_escape_valid" { | 1782 | test "json.test.i_string_incomplete_surrogate_and_escape_valid" { |
| 1783 | any( | 1783 | any( |
| 1784 | \\["\uD800\n"] | 1784 | \\["\uD800\n"] |
| 1785 | ); | 1785 | ); |
| 1786 | } | 1786 | } |
| 1787 | 1787 | ||
| 1788 | test "i_string_incomplete_surrogate_pair" { | 1788 | test "json.test.i_string_incomplete_surrogate_pair" { |
| 1789 | any( | 1789 | any( |
| 1790 | \\["\uDd1ea"] | 1790 | \\["\uDd1ea"] |
| 1791 | ); | 1791 | ); |
| 1792 | } | 1792 | } |
| 1793 | 1793 | ||
| 1794 | test "i_string_incomplete_surrogates_escape_valid" { | 1794 | test "json.test.i_string_incomplete_surrogates_escape_valid" { |
| 1795 | any( | 1795 | any( |
| 1796 | \\["\uD800\uD800\n"] | 1796 | \\["\uD800\uD800\n"] |
| 1797 | ); | 1797 | ); |
| 1798 | } | 1798 | } |
| 1799 | 1799 | ||
| 1800 | test "i_string_invalid_lonely_surrogate" { | 1800 | test "json.test.i_string_invalid_lonely_surrogate" { |
| 1801 | any( | 1801 | any( |
| 1802 | \\["\ud800"] | 1802 | \\["\ud800"] |
| 1803 | ); | 1803 | ); |
| 1804 | } | 1804 | } |
| 1805 | 1805 | ||
| 1806 | test "i_string_invalid_surrogate" { | 1806 | test "json.test.i_string_invalid_surrogate" { |
| 1807 | any( | 1807 | any( |
| 1808 | \\["\ud800abc"] | 1808 | \\["\ud800abc"] |
| 1809 | ); | 1809 | ); |
| 1810 | } | 1810 | } |
| 1811 | 1811 | ||
| 1812 | test "i_string_invalid_utf-8" { | 1812 | test "json.test.i_string_invalid_utf-8" { |
| 1813 | any( | 1813 | any( |
| 1814 | \\["ÿ"] | 1814 | \\["ÿ"] |
| 1815 | ); | 1815 | ); |
| 1816 | } | 1816 | } |
| 1817 | 1817 | ||
| 1818 | test "i_string_inverted_surrogates_U+1D11E" { | 1818 | test "json.test.i_string_inverted_surrogates_U+1D11E" { |
| 1819 | any( | 1819 | any( |
| 1820 | \\["\uDd1e\uD834"] | 1820 | \\["\uDd1e\uD834"] |
| 1821 | ); | 1821 | ); |
| 1822 | } | 1822 | } |
| 1823 | 1823 | ||
| 1824 | test "i_string_iso_latin_1" { | 1824 | test "json.test.i_string_iso_latin_1" { |
| 1825 | any( | 1825 | any( |
| 1826 | \\["é"] | 1826 | \\["é"] |
| 1827 | ); | 1827 | ); |
| 1828 | } | 1828 | } |
| 1829 | 1829 | ||
| 1830 | test "i_string_lone_second_surrogate" { | 1830 | test "json.test.i_string_lone_second_surrogate" { |
| 1831 | any( | 1831 | any( |
| 1832 | \\["\uDFAA"] | 1832 | \\["\uDFAA"] |
| 1833 | ); | 1833 | ); |
| 1834 | } | 1834 | } |
| 1835 | 1835 | ||
| 1836 | test "i_string_lone_utf8_continuation_byte" { | 1836 | test "json.test.i_string_lone_utf8_continuation_byte" { |
| 1837 | any( | 1837 | any( |
| 1838 | \\[""] | 1838 | \\[""] |
| 1839 | ); | 1839 | ); |
| 1840 | } | 1840 | } |
| 1841 | 1841 | ||
| 1842 | test "i_string_not_in_unicode_range" { | 1842 | test "json.test.i_string_not_in_unicode_range" { |
| 1843 | any( | 1843 | any( |
| 1844 | \\["ô¿¿¿"] | 1844 | \\["ô¿¿¿"] |
| 1845 | ); | 1845 | ); |
| 1846 | } | 1846 | } |
| 1847 | 1847 | ||
| 1848 | test "i_string_overlong_sequence_2_bytes" { | 1848 | test "json.test.i_string_overlong_sequence_2_bytes" { |
| 1849 | any( | 1849 | any( |
| 1850 | \\["À¯"] | 1850 | \\["À¯"] |
| 1851 | ); | 1851 | ); |
| 1852 | } | 1852 | } |
| 1853 | 1853 | ||
| 1854 | test "i_string_overlong_sequence_6_bytes" { | 1854 | test "json.test.i_string_overlong_sequence_6_bytes" { |
| 1855 | any( | 1855 | any( |
| 1856 | \\["ü¿¿¿¿"] | 1856 | \\["ü¿¿¿¿"] |
| 1857 | ); | 1857 | ); |
| 1858 | } | 1858 | } |
| 1859 | 1859 | ||
| 1860 | test "i_string_overlong_sequence_6_bytes_null" { | 1860 | test "json.test.i_string_overlong_sequence_6_bytes_null" { |
| 1861 | any( | 1861 | any( |
| 1862 | \\["ü"] | 1862 | \\["ü"] |
| 1863 | ); | 1863 | ); |
| 1864 | } | 1864 | } |
| 1865 | 1865 | ||
| 1866 | test "i_string_truncated-utf-8" { | 1866 | test "json.test.i_string_truncated-utf-8" { |
| 1867 | any( | 1867 | any( |
| 1868 | \\["àÿ"] | 1868 | \\["àÿ"] |
| 1869 | ); | 1869 | ); |
| 1870 | } | 1870 | } |
| 1871 | 1871 | ||
| 1872 | test "i_string_utf16BE_no_BOM" { | 1872 | test "json.test.i_string_utf16BE_no_BOM" { |
| 1873 | any("\x00\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d"); | 1873 | any("\x00\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d"); |
| 1874 | } | 1874 | } |
| 1875 | 1875 | ||
| 1876 | test "i_string_utf16LE_no_BOM" { | 1876 | test "json.test.i_string_utf16LE_no_BOM" { |
| 1877 | any("\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d\x00"); | 1877 | any("\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d\x00"); |
| 1878 | } | 1878 | } |
| 1879 | 1879 | ||
| 1880 | test "i_string_UTF-16LE_with_BOM" { | 1880 | test "json.test.i_string_UTF-16LE_with_BOM" { |
| 1881 | any("\xc3\xbf\xc3\xbe\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d\x00"); | 1881 | any("\xc3\xbf\xc3\xbe\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d\x00"); |
| 1882 | } | 1882 | } |
| 1883 | 1883 | ||
| 1884 | test "i_string_UTF-8_invalid_sequence" { | 1884 | test "json.test.i_string_UTF-8_invalid_sequence" { |
| 1885 | any( | 1885 | any( |
| 1886 | \\["æ¥Ñú"] | 1886 | \\["æ¥Ñú"] |
| 1887 | ); | 1887 | ); |
| 1888 | } | 1888 | } |
| 1889 | 1889 | ||
| 1890 | test "i_string_UTF8_surrogate_U+D800" { | 1890 | test "json.test.i_string_UTF8_surrogate_U+D800" { |
| 1891 | any( | 1891 | any( |
| 1892 | \\["í "] | 1892 | \\["í "] |
| 1893 | ); | 1893 | ); |
| 1894 | } | 1894 | } |
| 1895 | 1895 | ||
| 1896 | test "i_structure_500_nested_arrays" { | 1896 | test "json.test.i_structure_500_nested_arrays" { |
| 1897 | any(("[" ** 500) ++ ("]" ** 500)); | 1897 | any(("[" ** 500) ++ ("]" ** 500)); |
| 1898 | } | 1898 | } |
| 1899 | 1899 | ||
| 1900 | test "i_structure_UTF-8_BOM_empty_object" { | 1900 | test "json.test.i_structure_UTF-8_BOM_empty_object" { |
| 1901 | any( | 1901 | any( |
| 1902 | \\{} | 1902 | \\{} |
| 1903 | ); | 1903 | ); |
std/math/index.zig+63| ... | @@ -365,6 +365,69 @@ pub fn Log2Int(comptime T: type) type { | ... | @@ -365,6 +365,69 @@ pub fn Log2Int(comptime T: type) type { |
| 365 | return @IntType(false, count); | 365 | return @IntType(false, count); |
| 366 | } | 366 | } |
| 367 | 367 | ||
| 368 | pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) type { | ||
| 369 | assert(from <= to); | ||
| 370 | if (from == 0 and to == 0) { | ||
| 371 | return u0; | ||
| 372 | } | ||
| 373 | const is_signed = from < 0; | ||
| 374 | const largest_positive_integer = max(if (from<0) (-from)-1 else from, to); // two's complement | ||
| 375 | const base = log2(largest_positive_integer); | ||
| 376 | const upper = (1 << base) - 1; | ||
| 377 | var magnitude_bits = if (upper >= largest_positive_integer) base else base + 1; | ||
| 378 | if (is_signed) { | ||
| 379 | magnitude_bits += 1; | ||
| 380 | } | ||
| 381 | return @IntType(is_signed, magnitude_bits); | ||
| 382 | } | ||
| 383 | |||
| 384 | test "math.IntFittingRange" { | ||
| 385 | assert(IntFittingRange(0, 0) == u0); | ||
| 386 | assert(IntFittingRange(0, 1) == u1); | ||
| 387 | assert(IntFittingRange(0, 2) == u2); | ||
| 388 | assert(IntFittingRange(0, 3) == u2); | ||
| 389 | assert(IntFittingRange(0, 4) == u3); | ||
| 390 | assert(IntFittingRange(0, 7) == u3); | ||
| 391 | assert(IntFittingRange(0, 8) == u4); | ||
| 392 | assert(IntFittingRange(0, 9) == u4); | ||
| 393 | assert(IntFittingRange(0, 15) == u4); | ||
| 394 | assert(IntFittingRange(0, 16) == u5); | ||
| 395 | assert(IntFittingRange(0, 17) == u5); | ||
| 396 | assert(IntFittingRange(0, 4095) == u12); | ||
| 397 | assert(IntFittingRange(2000, 4095) == u12); | ||
| 398 | assert(IntFittingRange(0, 4096) == u13); | ||
| 399 | assert(IntFittingRange(2000, 4096) == u13); | ||
| 400 | assert(IntFittingRange(0, 4097) == u13); | ||
| 401 | assert(IntFittingRange(2000, 4097) == u13); | ||
| 402 | assert(IntFittingRange(0, 123456789123456798123456789) == u87); | ||
| 403 | assert(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177); | ||
| 404 | |||
| 405 | assert(IntFittingRange(-1, -1) == i1); | ||
| 406 | assert(IntFittingRange(-1, 0) == i1); | ||
| 407 | assert(IntFittingRange(-1, 1) == i2); | ||
| 408 | assert(IntFittingRange(-2, -2) == i2); | ||
| 409 | assert(IntFittingRange(-2, -1) == i2); | ||
| 410 | assert(IntFittingRange(-2, 0) == i2); | ||
| 411 | assert(IntFittingRange(-2, 1) == i2); | ||
| 412 | assert(IntFittingRange(-2, 2) == i3); | ||
| 413 | assert(IntFittingRange(-1, 2) == i3); | ||
| 414 | assert(IntFittingRange(-1, 3) == i3); | ||
| 415 | assert(IntFittingRange(-1, 4) == i4); | ||
| 416 | assert(IntFittingRange(-1, 7) == i4); | ||
| 417 | assert(IntFittingRange(-1, 8) == i5); | ||
| 418 | assert(IntFittingRange(-1, 9) == i5); | ||
| 419 | assert(IntFittingRange(-1, 15) == i5); | ||
| 420 | assert(IntFittingRange(-1, 16) == i6); | ||
| 421 | assert(IntFittingRange(-1, 17) == i6); | ||
| 422 | assert(IntFittingRange(-1, 4095) == i13); | ||
| 423 | assert(IntFittingRange(-4096, 4095) == i13); | ||
| 424 | assert(IntFittingRange(-1, 4096) == i14); | ||
| 425 | assert(IntFittingRange(-4097, 4095) == i14); | ||
| 426 | assert(IntFittingRange(-1, 4097) == i14); | ||
| 427 | assert(IntFittingRange(-1, 123456789123456798123456789) == i88); | ||
| 428 | assert(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178); | ||
| 429 | } | ||
| 430 | |||
| 368 | test "math overflow functions" { | 431 | test "math overflow functions" { |
| 369 | testOverflow(); | 432 | testOverflow(); |
| 370 | comptime testOverflow(); | 433 | comptime testOverflow(); |
std/mem.zig+1-3| ... | @@ -546,9 +546,7 @@ pub fn writeIntLE(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void { | ... | @@ -546,9 +546,7 @@ pub fn writeIntLE(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void { |
| 546 | buf[0] = bits; | 546 | buf[0] = bits; |
| 547 | return; | 547 | return; |
| 548 | } | 548 | } |
| 549 | // FIXME: this should just be for (buf). | 549 | for (buf) |*b| { |
| 550 | // See https://github.com/ziglang/zig/issues/1663 | ||
| 551 | for (buf.*) |*b| { | ||
| 552 | b.* = @truncate(u8, bits); | 550 | b.* = @truncate(u8, bits); |
| 553 | bits >>= 8; | 551 | bits >>= 8; |
| 554 | } | 552 | } |
std/meta/index.zig+19| ... | @@ -76,6 +76,25 @@ test "std.meta.tagName" { | ... | @@ -76,6 +76,25 @@ test "std.meta.tagName" { |
| 76 | debug.assert(mem.eql(u8, tagName(u2b), "D")); | 76 | debug.assert(mem.eql(u8, tagName(u2b), "D")); |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | pub fn stringToEnum(comptime T: type, str: []const u8) ?T { | ||
| 80 | inline for (@typeInfo(T).Enum.fields) |enumField| { | ||
| 81 | if (std.mem.eql(u8, str, enumField.name)) { | ||
| 82 | return @field(T, enumField.name); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | return null; | ||
| 86 | } | ||
| 87 | |||
| 88 | test "std.meta.stringToEnum" { | ||
| 89 | const E1 = enum { | ||
| 90 | A, | ||
| 91 | B, | ||
| 92 | }; | ||
| 93 | debug.assert(E1.A == stringToEnum(E1, "A").?); | ||
| 94 | debug.assert(E1.B == stringToEnum(E1, "B").?); | ||
| 95 | debug.assert(null == stringToEnum(E1, "C")); | ||
| 96 | } | ||
| 97 | |||
| 79 | pub fn bitCount(comptime T: type) u32 { | 98 | pub fn bitCount(comptime T: type) u32 { |
| 80 | return switch (@typeInfo(T)) { | 99 | return switch (@typeInfo(T)) { |
| 81 | TypeId.Int => |info| info.bits, | 100 | TypeId.Int => |info| info.bits, |
std/os/linux/index.zig+56-47| ... | @@ -703,7 +703,7 @@ pub fn dup2(old: i32, new: i32) usize { | ... | @@ -703,7 +703,7 @@ pub fn dup2(old: i32, new: i32) usize { |
| 703 | } | 703 | } |
| 704 | 704 | ||
| 705 | pub fn dup3(old: i32, new: i32, flags: u32) usize { | 705 | pub fn dup3(old: i32, new: i32, flags: u32) usize { |
| 706 | return syscall3(SYS_dup3, @intCast(usize, old), @intCast(usize, new), flags); | 706 | return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags); |
| 707 | } | 707 | } |
| 708 | 708 | ||
| 709 | // TODO https://github.com/ziglang/zig/issues/265 | 709 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -747,7 +747,7 @@ pub fn getcwd(buf: [*]u8, size: usize) usize { | ... | @@ -747,7 +747,7 @@ pub fn getcwd(buf: [*]u8, size: usize) usize { |
| 747 | } | 747 | } |
| 748 | 748 | ||
| 749 | pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { | 749 | pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { |
| 750 | return syscall3(SYS_getdents64, @intCast(usize, fd), @ptrToInt(dirp), count); | 750 | return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); |
| 751 | } | 751 | } |
| 752 | 752 | ||
| 753 | pub fn inotify_init1(flags: u32) usize { | 753 | pub fn inotify_init1(flags: u32) usize { |
| ... | @@ -755,16 +755,16 @@ pub fn inotify_init1(flags: u32) usize { | ... | @@ -755,16 +755,16 @@ pub fn inotify_init1(flags: u32) usize { |
| 755 | } | 755 | } |
| 756 | 756 | ||
| 757 | pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { | 757 | pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { |
| 758 | return syscall3(SYS_inotify_add_watch, @intCast(usize, fd), @ptrToInt(pathname), mask); | 758 | return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask); |
| 759 | } | 759 | } |
| 760 | 760 | ||
| 761 | pub fn inotify_rm_watch(fd: i32, wd: i32) usize { | 761 | pub fn inotify_rm_watch(fd: i32, wd: i32) usize { |
| 762 | return syscall2(SYS_inotify_rm_watch, @intCast(usize, fd), @intCast(usize, wd)); | 762 | return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd))); |
| 763 | } | 763 | } |
| 764 | 764 | ||
| 765 | pub fn isatty(fd: i32) bool { | 765 | pub fn isatty(fd: i32) bool { |
| 766 | var wsz: winsize = undefined; | 766 | var wsz: winsize = undefined; |
| 767 | return syscall3(SYS_ioctl, @intCast(usize, fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; | 767 | return syscall3(SYS_ioctl, @bitCast(usize, isize(fd)), TIOCGWINSZ, @ptrToInt(&wsz)) == 0; |
| 768 | } | 768 | } |
| 769 | 769 | ||
| 770 | // TODO https://github.com/ziglang/zig/issues/265 | 770 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -774,7 +774,7 @@ pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usiz | ... | @@ -774,7 +774,7 @@ pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usiz |
| 774 | 774 | ||
| 775 | // TODO https://github.com/ziglang/zig/issues/265 | 775 | // TODO https://github.com/ziglang/zig/issues/265 |
| 776 | pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { | 776 | pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { |
| 777 | return syscall4(SYS_readlinkat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); | 777 | return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); |
| 778 | } | 778 | } |
| 779 | 779 | ||
| 780 | // TODO https://github.com/ziglang/zig/issues/265 | 780 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -784,7 +784,7 @@ pub fn mkdir(path: [*]const u8, mode: u32) usize { | ... | @@ -784,7 +784,7 @@ pub fn mkdir(path: [*]const u8, mode: u32) usize { |
| 784 | 784 | ||
| 785 | // TODO https://github.com/ziglang/zig/issues/265 | 785 | // TODO https://github.com/ziglang/zig/issues/265 |
| 786 | pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { | 786 | pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { |
| 787 | return syscall3(SYS_mkdirat, @intCast(usize, dirfd), @ptrToInt(path), mode); | 787 | return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); |
| 788 | } | 788 | } |
| 789 | 789 | ||
| 790 | // TODO https://github.com/ziglang/zig/issues/265 | 790 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -803,7 +803,7 @@ pub fn umount2(special: [*]const u8, flags: u32) usize { | ... | @@ -803,7 +803,7 @@ pub fn umount2(special: [*]const u8, flags: u32) usize { |
| 803 | } | 803 | } |
| 804 | 804 | ||
| 805 | pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { | 805 | pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { |
| 806 | return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset)); | 806 | return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); |
| 807 | } | 807 | } |
| 808 | 808 | ||
| 809 | pub fn munmap(address: usize, length: usize) usize { | 809 | pub fn munmap(address: usize, length: usize) usize { |
| ... | @@ -811,23 +811,23 @@ pub fn munmap(address: usize, length: usize) usize { | ... | @@ -811,23 +811,23 @@ pub fn munmap(address: usize, length: usize) usize { |
| 811 | } | 811 | } |
| 812 | 812 | ||
| 813 | pub fn read(fd: i32, buf: [*]u8, count: usize) usize { | 813 | pub fn read(fd: i32, buf: [*]u8, count: usize) usize { |
| 814 | return syscall3(SYS_read, @intCast(usize, fd), @ptrToInt(buf), count); | 814 | return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); |
| 815 | } | 815 | } |
| 816 | 816 | ||
| 817 | pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { | 817 | pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { |
| 818 | return syscall4(SYS_preadv, @intCast(usize, fd), @ptrToInt(iov), count, offset); | 818 | return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); |
| 819 | } | 819 | } |
| 820 | 820 | ||
| 821 | pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { | 821 | pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { |
| 822 | return syscall3(SYS_readv, @intCast(usize, fd), @ptrToInt(iov), count); | 822 | return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); |
| 823 | } | 823 | } |
| 824 | 824 | ||
| 825 | pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { | 825 | pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { |
| 826 | return syscall3(SYS_writev, @intCast(usize, fd), @ptrToInt(iov), count); | 826 | return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); |
| 827 | } | 827 | } |
| 828 | 828 | ||
| 829 | pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { | 829 | pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { |
| 830 | return syscall4(SYS_pwritev, @intCast(usize, fd), @ptrToInt(iov), count, offset); | 830 | return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); |
| 831 | } | 831 | } |
| 832 | 832 | ||
| 833 | // TODO https://github.com/ziglang/zig/issues/265 | 833 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -842,12 +842,12 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { | ... | @@ -842,12 +842,12 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { |
| 842 | 842 | ||
| 843 | // TODO https://github.com/ziglang/zig/issues/265 | 843 | // TODO https://github.com/ziglang/zig/issues/265 |
| 844 | pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { | 844 | pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { |
| 845 | return syscall3(SYS_symlinkat, @ptrToInt(existing), @intCast(usize, newfd), @ptrToInt(newpath)); | 845 | return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath)); |
| 846 | } | 846 | } |
| 847 | 847 | ||
| 848 | // TODO https://github.com/ziglang/zig/issues/265 | 848 | // TODO https://github.com/ziglang/zig/issues/265 |
| 849 | pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { | 849 | pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { |
| 850 | return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset); | 850 | return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); |
| 851 | } | 851 | } |
| 852 | 852 | ||
| 853 | // TODO https://github.com/ziglang/zig/issues/265 | 853 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -856,7 +856,7 @@ pub fn access(path: [*]const u8, mode: u32) usize { | ... | @@ -856,7 +856,7 @@ pub fn access(path: [*]const u8, mode: u32) usize { |
| 856 | } | 856 | } |
| 857 | 857 | ||
| 858 | pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { | 858 | pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { |
| 859 | return syscall3(SYS_faccessat, @intCast(usize, dirfd), @ptrToInt(path), mode); | 859 | return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); |
| 860 | } | 860 | } |
| 861 | 861 | ||
| 862 | pub fn pipe(fd: *[2]i32) usize { | 862 | pub fn pipe(fd: *[2]i32) usize { |
| ... | @@ -868,11 +868,11 @@ pub fn pipe2(fd: *[2]i32, flags: u32) usize { | ... | @@ -868,11 +868,11 @@ pub fn pipe2(fd: *[2]i32, flags: u32) usize { |
| 868 | } | 868 | } |
| 869 | 869 | ||
| 870 | pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { | 870 | pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { |
| 871 | return syscall3(SYS_write, @intCast(usize, fd), @ptrToInt(buf), count); | 871 | return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); |
| 872 | } | 872 | } |
| 873 | 873 | ||
| 874 | pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { | 874 | pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { |
| 875 | return syscall4(SYS_pwrite, @intCast(usize, fd), @ptrToInt(buf), count, offset); | 875 | return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); |
| 876 | } | 876 | } |
| 877 | 877 | ||
| 878 | // TODO https://github.com/ziglang/zig/issues/265 | 878 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -882,7 +882,7 @@ pub fn rename(old: [*]const u8, new: [*]const u8) usize { | ... | @@ -882,7 +882,7 @@ pub fn rename(old: [*]const u8, new: [*]const u8) usize { |
| 882 | 882 | ||
| 883 | // TODO https://github.com/ziglang/zig/issues/265 | 883 | // TODO https://github.com/ziglang/zig/issues/265 |
| 884 | pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { | 884 | pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { |
| 885 | return syscall5(SYS_renameat2, @intCast(usize, oldfd), @ptrToInt(oldpath), @intCast(usize, newfd), @ptrToInt(newpath), flags); | 885 | return syscall5(SYS_renameat2, @bitCast(usize, isize(oldfd)), @ptrToInt(oldpath), @bitCast(usize, isize(newfd)), @ptrToInt(newpath), flags); |
| 886 | } | 886 | } |
| 887 | 887 | ||
| 888 | // TODO https://github.com/ziglang/zig/issues/265 | 888 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -897,7 +897,8 @@ pub fn create(path: [*]const u8, perm: usize) usize { | ... | @@ -897,7 +897,8 @@ pub fn create(path: [*]const u8, perm: usize) usize { |
| 897 | 897 | ||
| 898 | // TODO https://github.com/ziglang/zig/issues/265 | 898 | // TODO https://github.com/ziglang/zig/issues/265 |
| 899 | pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { | 899 | pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { |
| 900 | return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode); | 900 | // dirfd could be negative, for example AT_FDCWD is -100 |
| 901 | return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode); | ||
| 901 | } | 902 | } |
| 902 | 903 | ||
| 903 | /// See also `clone` (from the arch-specific include) | 904 | /// See also `clone` (from the arch-specific include) |
| ... | @@ -911,11 +912,11 @@ pub fn clone2(flags: u32, child_stack_ptr: usize) usize { | ... | @@ -911,11 +912,11 @@ pub fn clone2(flags: u32, child_stack_ptr: usize) usize { |
| 911 | } | 912 | } |
| 912 | 913 | ||
| 913 | pub fn close(fd: i32) usize { | 914 | pub fn close(fd: i32) usize { |
| 914 | return syscall1(SYS_close, @intCast(usize, fd)); | 915 | return syscall1(SYS_close, @bitCast(usize, isize(fd))); |
| 915 | } | 916 | } |
| 916 | 917 | ||
| 917 | pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize { | 918 | pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize { |
| 918 | return syscall3(SYS_lseek, @intCast(usize, fd), @bitCast(usize, offset), ref_pos); | 919 | return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), ref_pos); |
| 919 | } | 920 | } |
| 920 | 921 | ||
| 921 | pub fn exit(status: i32) noreturn { | 922 | pub fn exit(status: i32) noreturn { |
| ... | @@ -933,7 +934,7 @@ pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { | ... | @@ -933,7 +934,7 @@ pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { |
| 933 | } | 934 | } |
| 934 | 935 | ||
| 935 | pub fn kill(pid: i32, sig: i32) usize { | 936 | pub fn kill(pid: i32, sig: i32) usize { |
| 936 | return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @intCast(usize, sig)); | 937 | return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig))); |
| 937 | } | 938 | } |
| 938 | 939 | ||
| 939 | // TODO https://github.com/ziglang/zig/issues/265 | 940 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -943,7 +944,7 @@ pub fn unlink(path: [*]const u8) usize { | ... | @@ -943,7 +944,7 @@ pub fn unlink(path: [*]const u8) usize { |
| 943 | 944 | ||
| 944 | // TODO https://github.com/ziglang/zig/issues/265 | 945 | // TODO https://github.com/ziglang/zig/issues/265 |
| 945 | pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { | 946 | pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { |
| 946 | return syscall3(SYS_unlinkat, @intCast(usize, dirfd), @ptrToInt(path), flags); | 947 | return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); |
| 947 | } | 948 | } |
| 948 | 949 | ||
| 949 | pub fn waitpid(pid: i32, status: *i32, options: i32) usize { | 950 | pub fn waitpid(pid: i32, status: *i32, options: i32) usize { |
| ... | @@ -1120,8 +1121,8 @@ pub const empty_sigset = []usize{0} ** sigset_t.len; | ... | @@ -1120,8 +1121,8 @@ pub const empty_sigset = []usize{0} ** sigset_t.len; |
| 1120 | pub fn raise(sig: i32) usize { | 1121 | pub fn raise(sig: i32) usize { |
| 1121 | var set: sigset_t = undefined; | 1122 | var set: sigset_t = undefined; |
| 1122 | blockAppSignals(&set); | 1123 | blockAppSignals(&set); |
| 1123 | const tid = @intCast(i32, syscall0(SYS_gettid)); | 1124 | const tid = syscall0(SYS_gettid); |
| 1124 | const ret = syscall2(SYS_tkill, @intCast(usize, tid), @intCast(usize, sig)); | 1125 | const ret = syscall2(SYS_tkill, tid, @bitCast(usize, isize(sig))); |
| 1125 | restoreSignals(&set); | 1126 | restoreSignals(&set); |
| 1126 | return ret; | 1127 | return ret; |
| 1127 | } | 1128 | } |
| ... | @@ -1189,11 +1190,11 @@ pub const iovec_const = extern struct { | ... | @@ -1189,11 +1190,11 @@ pub const iovec_const = extern struct { |
| 1189 | }; | 1190 | }; |
| 1190 | 1191 | ||
| 1191 | pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { | 1192 | pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { |
| 1192 | return syscall3(SYS_getsockname, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len)); | 1193 | return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); |
| 1193 | } | 1194 | } |
| 1194 | 1195 | ||
| 1195 | pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { | 1196 | pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { |
| 1196 | return syscall3(SYS_getpeername, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len)); | 1197 | return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); |
| 1197 | } | 1198 | } |
| 1198 | 1199 | ||
| 1199 | pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { | 1200 | pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { |
| ... | @@ -1201,47 +1202,47 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { | ... | @@ -1201,47 +1202,47 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { |
| 1201 | } | 1202 | } |
| 1202 | 1203 | ||
| 1203 | pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { | 1204 | pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { |
| 1204 | return syscall5(SYS_setsockopt, @intCast(usize, fd), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); | 1205 | return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); |
| 1205 | } | 1206 | } |
| 1206 | 1207 | ||
| 1207 | pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { | 1208 | pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { |
| 1208 | return syscall5(SYS_getsockopt, @intCast(usize, fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); | 1209 | return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); |
| 1209 | } | 1210 | } |
| 1210 | 1211 | ||
| 1211 | pub fn sendmsg(fd: i32, msg: *const msghdr, flags: u32) usize { | 1212 | pub fn sendmsg(fd: i32, msg: *const msghdr, flags: u32) usize { |
| 1212 | return syscall3(SYS_sendmsg, @intCast(usize, fd), @ptrToInt(msg), flags); | 1213 | return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); |
| 1213 | } | 1214 | } |
| 1214 | 1215 | ||
| 1215 | pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { | 1216 | pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { |
| 1216 | return syscall3(SYS_connect, @intCast(usize, fd), @ptrToInt(addr), len); | 1217 | return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len); |
| 1217 | } | 1218 | } |
| 1218 | 1219 | ||
| 1219 | pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { | 1220 | pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { |
| 1220 | return syscall3(SYS_recvmsg, @intCast(usize, fd), @ptrToInt(msg), flags); | 1221 | return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); |
| 1221 | } | 1222 | } |
| 1222 | 1223 | ||
| 1223 | pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { | 1224 | pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { |
| 1224 | return syscall6(SYS_recvfrom, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); | 1225 | return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); |
| 1225 | } | 1226 | } |
| 1226 | 1227 | ||
| 1227 | pub fn shutdown(fd: i32, how: i32) usize { | 1228 | pub fn shutdown(fd: i32, how: i32) usize { |
| 1228 | return syscall2(SYS_shutdown, @intCast(usize, fd), @intCast(usize, how)); | 1229 | return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how))); |
| 1229 | } | 1230 | } |
| 1230 | 1231 | ||
| 1231 | pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { | 1232 | pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { |
| 1232 | return syscall3(SYS_bind, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len)); | 1233 | return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len)); |
| 1233 | } | 1234 | } |
| 1234 | 1235 | ||
| 1235 | pub fn listen(fd: i32, backlog: u32) usize { | 1236 | pub fn listen(fd: i32, backlog: u32) usize { |
| 1236 | return syscall2(SYS_listen, @intCast(usize, fd), backlog); | 1237 | return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog); |
| 1237 | } | 1238 | } |
| 1238 | 1239 | ||
| 1239 | pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { | 1240 | pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { |
| 1240 | return syscall6(SYS_sendto, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); | 1241 | return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); |
| 1241 | } | 1242 | } |
| 1242 | 1243 | ||
| 1243 | pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { | 1244 | pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { |
| 1244 | return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(*fd[0])); | 1245 | return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0])); |
| 1245 | } | 1246 | } |
| 1246 | 1247 | ||
| 1247 | pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { | 1248 | pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { |
| ... | @@ -1249,11 +1250,11 @@ pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { | ... | @@ -1249,11 +1250,11 @@ pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { |
| 1249 | } | 1250 | } |
| 1250 | 1251 | ||
| 1251 | pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize { | 1252 | pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize { |
| 1252 | return syscall4(SYS_accept4, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len), flags); | 1253 | return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags); |
| 1253 | } | 1254 | } |
| 1254 | 1255 | ||
| 1255 | pub fn fstat(fd: i32, stat_buf: *Stat) usize { | 1256 | pub fn fstat(fd: i32, stat_buf: *Stat) usize { |
| 1256 | return syscall2(SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf)); | 1257 | return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); |
| 1257 | } | 1258 | } |
| 1258 | 1259 | ||
| 1259 | // TODO https://github.com/ziglang/zig/issues/265 | 1260 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -1268,7 +1269,7 @@ pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { | ... | @@ -1268,7 +1269,7 @@ pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { |
| 1268 | 1269 | ||
| 1269 | // TODO https://github.com/ziglang/zig/issues/265 | 1270 | // TODO https://github.com/ziglang/zig/issues/265 |
| 1270 | pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { | 1271 | pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { |
| 1271 | return syscall4(SYS_fstatat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(stat_buf), flags); | 1272 | return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); |
| 1272 | } | 1273 | } |
| 1273 | 1274 | ||
| 1274 | // TODO https://github.com/ziglang/zig/issues/265 | 1275 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | @@ -1355,7 +1356,7 @@ pub fn epoll_create1(flags: usize) usize { | ... | @@ -1355,7 +1356,7 @@ pub fn epoll_create1(flags: usize) usize { |
| 1355 | } | 1356 | } |
| 1356 | 1357 | ||
| 1357 | pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { | 1358 | pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { |
| 1358 | return syscall4(SYS_epoll_ctl, @intCast(usize, epoll_fd), @intCast(usize, op), @intCast(usize, fd), @ptrToInt(ev)); | 1359 | return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev)); |
| 1359 | } | 1360 | } |
| 1360 | 1361 | ||
| 1361 | pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { | 1362 | pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { |
| ... | @@ -1363,7 +1364,15 @@ pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout | ... | @@ -1363,7 +1364,15 @@ pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout |
| 1363 | } | 1364 | } |
| 1364 | 1365 | ||
| 1365 | pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { | 1366 | pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { |
| 1366 | return syscall6(SYS_epoll_pwait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout), @ptrToInt(sigmask), @sizeOf(sigset_t)); | 1367 | return syscall6( |
| 1368 | SYS_epoll_pwait, | ||
| 1369 | @bitCast(usize, isize(epoll_fd)), | ||
| 1370 | @ptrToInt(events), | ||
| 1371 | @intCast(usize, maxevents), | ||
| 1372 | @bitCast(usize, isize(timeout)), | ||
| 1373 | @ptrToInt(sigmask), | ||
| 1374 | @sizeOf(sigset_t), | ||
| 1375 | ); | ||
| 1367 | } | 1376 | } |
| 1368 | 1377 | ||
| 1369 | pub fn eventfd(count: u32, flags: u32) usize { | 1378 | pub fn eventfd(count: u32, flags: u32) usize { |
| ... | @@ -1371,7 +1380,7 @@ pub fn eventfd(count: u32, flags: u32) usize { | ... | @@ -1371,7 +1380,7 @@ pub fn eventfd(count: u32, flags: u32) usize { |
| 1371 | } | 1380 | } |
| 1372 | 1381 | ||
| 1373 | pub fn timerfd_create(clockid: i32, flags: u32) usize { | 1382 | pub fn timerfd_create(clockid: i32, flags: u32) usize { |
| 1374 | return syscall2(SYS_timerfd_create, @intCast(usize, clockid), flags); | 1383 | return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags); |
| 1375 | } | 1384 | } |
| 1376 | 1385 | ||
| 1377 | pub const itimerspec = extern struct { | 1386 | pub const itimerspec = extern struct { |
| ... | @@ -1380,11 +1389,11 @@ pub const itimerspec = extern struct { | ... | @@ -1380,11 +1389,11 @@ pub const itimerspec = extern struct { |
| 1380 | }; | 1389 | }; |
| 1381 | 1390 | ||
| 1382 | pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { | 1391 | pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { |
| 1383 | return syscall2(SYS_timerfd_gettime, @intCast(usize, fd), @ptrToInt(curr_value)); | 1392 | return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value)); |
| 1384 | } | 1393 | } |
| 1385 | 1394 | ||
| 1386 | pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { | 1395 | pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { |
| 1387 | return syscall4(SYS_timerfd_settime, @intCast(usize, fd), flags, @ptrToInt(new_value), @ptrToInt(old_value)); | 1396 | return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); |
| 1388 | } | 1397 | } |
| 1389 | 1398 | ||
| 1390 | pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330; | 1399 | pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330; |
std/os/path.zig+1-1| ... | @@ -1183,7 +1183,7 @@ pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealErro | ... | @@ -1183,7 +1183,7 @@ pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealErro |
| 1183 | const fd = try os.posixOpenC(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); | 1183 | const fd = try os.posixOpenC(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); |
| 1184 | defer os.close(fd); | 1184 | defer os.close(fd); |
| 1185 | 1185 | ||
| 1186 | var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined; | 1186 | var buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined; |
| 1187 | const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}\x00", fd) catch unreachable; | 1187 | const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}\x00", fd) catch unreachable; |
| 1188 | 1188 | ||
| 1189 | return os.readLinkC(out_buffer, proc_path.ptr); | 1189 | return os.readLinkC(out_buffer, proc_path.ptr); |
std/os/zen.zig+11-11| ... | @@ -12,20 +12,20 @@ pub const Message = struct { | ... | @@ -12,20 +12,20 @@ pub const Message = struct { |
| 12 | args: [5]usize, | 12 | args: [5]usize, |
| 13 | payload: ?[]const u8, | 13 | payload: ?[]const u8, |
| 14 | 14 | ||
| 15 | pub fn from(mailbox_id: *const MailboxId) Message { | 15 | pub fn from(mailbox_id: MailboxId) Message { |
| 16 | return Message{ | 16 | return Message{ |
| 17 | .sender = MailboxId.Undefined, | 17 | .sender = MailboxId.Undefined, |
| 18 | .receiver = mailbox_id.*, | 18 | .receiver = mailbox_id, |
| 19 | .code = undefined, | 19 | .code = undefined, |
| 20 | .args = undefined, | 20 | .args = undefined, |
| 21 | .payload = null, | 21 | .payload = null, |
| 22 | }; | 22 | }; |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | pub fn to(mailbox_id: *const MailboxId, msg_code: usize, args: ...) Message { | 25 | pub fn to(mailbox_id: MailboxId, msg_code: usize, args: ...) Message { |
| 26 | var message = Message{ | 26 | var message = Message{ |
| 27 | .sender = MailboxId.This, | 27 | .sender = MailboxId.This, |
| 28 | .receiver = mailbox_id.*, | 28 | .receiver = mailbox_id, |
| 29 | .code = msg_code, | 29 | .code = msg_code, |
| 30 | .args = undefined, | 30 | .args = undefined, |
| 31 | .payload = null, | 31 | .payload = null, |
| ... | @@ -40,14 +40,14 @@ pub const Message = struct { | ... | @@ -40,14 +40,14 @@ pub const Message = struct { |
| 40 | return message; | 40 | return message; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | pub fn as(self: *const Message, sender: *const MailboxId) Message { | 43 | pub fn as(self: Message, sender: MailboxId) Message { |
| 44 | var message = self.*; | 44 | var message = self; |
| 45 | message.sender = sender.*; | 45 | message.sender = sender; |
| 46 | return message; | 46 | return message; |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | pub fn withPayload(self: *const Message, payload: []const u8) Message { | 49 | pub fn withPayload(self: Message, payload: []const u8) Message { |
| 50 | var message = self.*; | 50 | var message = self; |
| 51 | message.payload = payload; | 51 | message.payload = payload; |
| 52 | return message; | 52 | return message; |
| 53 | } | 53 | } |
| ... | @@ -93,7 +93,7 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize { | ... | @@ -93,7 +93,7 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize { |
| 93 | STDIN_FILENO => { | 93 | STDIN_FILENO => { |
| 94 | var i: usize = 0; | 94 | var i: usize = 0; |
| 95 | while (i < count) : (i += 1) { | 95 | while (i < count) : (i += 1) { |
| 96 | send(Message.to(Server.Keyboard, 0)); | 96 | send(&Message.to(Server.Keyboard, 0)); |
| 97 | 97 | ||
| 98 | // FIXME: we should be certain that we are receiving from Keyboard. | 98 | // FIXME: we should be certain that we are receiving from Keyboard. |
| 99 | var message = Message.from(MailboxId.This); | 99 | var message = Message.from(MailboxId.This); |
| ... | @@ -111,7 +111,7 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize { | ... | @@ -111,7 +111,7 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize { |
| 111 | pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { | 111 | pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { |
| 112 | switch (fd) { | 112 | switch (fd) { |
| 113 | STDOUT_FILENO, STDERR_FILENO => { | 113 | STDOUT_FILENO, STDERR_FILENO => { |
| 114 | send(Message.to(Server.Terminal, 1).withPayload(buf[0..count])); | 114 | send(&Message.to(Server.Terminal, 1).withPayload(buf[0..count])); |
| 115 | }, | 115 | }, |
| 116 | else => unreachable, | 116 | else => unreachable, |
| 117 | } | 117 | } |
std/pdb.zig+1| ... | @@ -34,6 +34,7 @@ pub const DbiStreamHeader = packed struct { | ... | @@ -34,6 +34,7 @@ pub const DbiStreamHeader = packed struct { |
| 34 | }; | 34 | }; |
| 35 | 35 | ||
| 36 | pub const SectionContribEntry = packed struct { | 36 | pub const SectionContribEntry = packed struct { |
| 37 | /// COFF Section index, 1-based | ||
| 37 | Section: u16, | 38 | Section: u16, |
| 38 | Padding1: [2]u8, | 39 | Padding1: [2]u8, |
| 39 | Offset: u32, | 40 | Offset: u32, |
std/rand/index.zig+155-40| ... | @@ -57,6 +57,18 @@ pub const Random = struct { | ... | @@ -57,6 +57,18 @@ pub const Random = struct { |
| 57 | return @bitCast(T, unsigned_result); | 57 | return @bitCast(T, unsigned_result); |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | /// Constant-time implementation off ::uintLessThan. | ||
| 61 | /// The results of this function may be biased. | ||
| 62 | pub fn uintLessThanBiased(r: *Random, comptime T: type, less_than: T) T { | ||
| 63 | comptime assert(T.is_signed == false); | ||
| 64 | comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation! | ||
| 65 | assert(0 < less_than); | ||
| 66 | if (T.bit_count <= 32) { | ||
| 67 | return @intCast(T, limitRangeBiased(u32, r.int(u32), less_than)); | ||
| 68 | } else { | ||
| 69 | return @intCast(T, limitRangeBiased(u64, r.int(u64), less_than)); | ||
| 70 | } | ||
| 71 | } | ||
| 60 | /// Returns an evenly distributed random unsigned integer `0 <= i < less_than`. | 72 | /// Returns an evenly distributed random unsigned integer `0 <= i < less_than`. |
| 61 | /// This function assumes that the underlying ::fillFn produces evenly distributed values. | 73 | /// This function assumes that the underlying ::fillFn produces evenly distributed values. |
| 62 | /// Within this assumption, the runtime of this function is exponentially distributed. | 74 | /// Within this assumption, the runtime of this function is exponentially distributed. |
| ... | @@ -64,29 +76,53 @@ pub const Random = struct { | ... | @@ -64,29 +76,53 @@ pub const Random = struct { |
| 64 | /// the runtime of this function would technically be unbounded. | 76 | /// the runtime of this function would technically be unbounded. |
| 65 | /// However, if ::fillFn is backed by any evenly distributed pseudo random number generator, | 77 | /// However, if ::fillFn is backed by any evenly distributed pseudo random number generator, |
| 66 | /// this function is guaranteed to return. | 78 | /// this function is guaranteed to return. |
| 67 | /// If you need deterministic runtime bounds, consider instead using `r.int(T) % less_than`, | 79 | /// If you need deterministic runtime bounds, use `::uintLessThanBiased`. |
| 68 | /// which will usually be biased toward smaller values. | ||
| 69 | pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T { | 80 | pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T { |
| 70 | assert(T.is_signed == false); | 81 | comptime assert(T.is_signed == false); |
| 82 | comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation! | ||
| 71 | assert(0 < less_than); | 83 | assert(0 < less_than); |
| 72 | 84 | // Small is typically u32 | |
| 73 | const last_group_size_minus_one: T = maxInt(T) % less_than; | 85 | const Small = @IntType(false, @divTrunc(T.bit_count + 31, 32) * 32); |
| 74 | if (last_group_size_minus_one == less_than - 1) { | 86 | // Large is typically u64 |
| 75 | // less_than is a power of two. | 87 | const Large = @IntType(false, Small.bit_count * 2); |
| 76 | assert(math.floorPowerOfTwo(T, less_than) == less_than); | 88 | |
| 77 | // There is no retry zone. The optimal retry_zone_start would be maxInt(T) + 1. | 89 | // adapted from: |
| 78 | return r.int(T) % less_than; | 90 | // http://www.pcg-random.org/posts/bounded-rands.html |
| 79 | } | 91 | // "Lemire's (with an extra tweak from me)" |
| 80 | const retry_zone_start = maxInt(T) - last_group_size_minus_one; | 92 | var x: Small = r.int(Small); |
| 81 | 93 | var m: Large = Large(x) * Large(less_than); | |
| 82 | while (true) { | 94 | var l: Small = @truncate(Small, m); |
| 83 | const rand_val = r.int(T); | 95 | if (l < less_than) { |
| 84 | if (rand_val < retry_zone_start) { | 96 | // TODO: workaround for https://github.com/ziglang/zig/issues/1770 |
| 85 | return rand_val % less_than; | 97 | // should be: |
| 98 | // var t: Small = -%less_than; | ||
| 99 | var t: Small = @bitCast(Small, -%@bitCast(@IntType(true, Small.bit_count), Small(less_than))); | ||
| 100 | |||
| 101 | if (t >= less_than) { | ||
| 102 | t -= less_than; | ||
| 103 | if (t >= less_than) { | ||
| 104 | t %= less_than; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | while (l < t) { | ||
| 108 | x = r.int(Small); | ||
| 109 | m = Large(x) * Large(less_than); | ||
| 110 | l = @truncate(Small, m); | ||
| 86 | } | 111 | } |
| 87 | } | 112 | } |
| 113 | return @intCast(T, m >> Small.bit_count); | ||
| 88 | } | 114 | } |
| 89 | 115 | ||
| 116 | /// Constant-time implementation off ::uintAtMost. | ||
| 117 | /// The results of this function may be biased. | ||
| 118 | pub fn uintAtMostBiased(r: *Random, comptime T: type, at_most: T) T { | ||
| 119 | assert(T.is_signed == false); | ||
| 120 | if (at_most == maxInt(T)) { | ||
| 121 | // have the full range | ||
| 122 | return r.int(T); | ||
| 123 | } | ||
| 124 | return r.uintLessThanBiased(T, at_most + 1); | ||
| 125 | } | ||
| 90 | /// Returns an evenly distributed random unsigned integer `0 <= i <= at_most`. | 126 | /// Returns an evenly distributed random unsigned integer `0 <= i <= at_most`. |
| 91 | /// See ::uintLessThan, which this function uses in most cases, | 127 | /// See ::uintLessThan, which this function uses in most cases, |
| 92 | /// for commentary on the runtime of this function. | 128 | /// for commentary on the runtime of this function. |
| ... | @@ -99,6 +135,22 @@ pub const Random = struct { | ... | @@ -99,6 +135,22 @@ pub const Random = struct { |
| 99 | return r.uintLessThan(T, at_most + 1); | 135 | return r.uintLessThan(T, at_most + 1); |
| 100 | } | 136 | } |
| 101 | 137 | ||
| 138 | /// Constant-time implementation off ::intRangeLessThan. | ||
| 139 | /// The results of this function may be biased. | ||
| 140 | pub fn intRangeLessThanBiased(r: *Random, comptime T: type, at_least: T, less_than: T) T { | ||
| 141 | assert(at_least < less_than); | ||
| 142 | if (T.is_signed) { | ||
| 143 | // Two's complement makes this math pretty easy. | ||
| 144 | const UnsignedT = @IntType(false, T.bit_count); | ||
| 145 | const lo = @bitCast(UnsignedT, at_least); | ||
| 146 | const hi = @bitCast(UnsignedT, less_than); | ||
| 147 | const result = lo +% r.uintLessThanBiased(UnsignedT, hi -% lo); | ||
| 148 | return @bitCast(T, result); | ||
| 149 | } else { | ||
| 150 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. | ||
| 151 | return at_least + r.uintLessThanBiased(T, less_than - at_least); | ||
| 152 | } | ||
| 153 | } | ||
| 102 | /// Returns an evenly distributed random integer `at_least <= i < less_than`. | 154 | /// Returns an evenly distributed random integer `at_least <= i < less_than`. |
| 103 | /// See ::uintLessThan, which this function uses in most cases, | 155 | /// See ::uintLessThan, which this function uses in most cases, |
| 104 | /// for commentary on the runtime of this function. | 156 | /// for commentary on the runtime of this function. |
| ... | @@ -117,6 +169,22 @@ pub const Random = struct { | ... | @@ -117,6 +169,22 @@ pub const Random = struct { |
| 117 | } | 169 | } |
| 118 | } | 170 | } |
| 119 | 171 | ||
| 172 | /// Constant-time implementation off ::intRangeAtMostBiased. | ||
| 173 | /// The results of this function may be biased. | ||
| 174 | pub fn intRangeAtMostBiased(r: *Random, comptime T: type, at_least: T, at_most: T) T { | ||
| 175 | assert(at_least <= at_most); | ||
| 176 | if (T.is_signed) { | ||
| 177 | // Two's complement makes this math pretty easy. | ||
| 178 | const UnsignedT = @IntType(false, T.bit_count); | ||
| 179 | const lo = @bitCast(UnsignedT, at_least); | ||
| 180 | const hi = @bitCast(UnsignedT, at_most); | ||
| 181 | const result = lo +% r.uintAtMostBiased(UnsignedT, hi -% lo); | ||
| 182 | return @bitCast(T, result); | ||
| 183 | } else { | ||
| 184 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. | ||
| 185 | return at_least + r.uintAtMostBiased(T, at_most - at_least); | ||
| 186 | } | ||
| 187 | } | ||
| 120 | /// Returns an evenly distributed random integer `at_least <= i <= at_most`. | 188 | /// Returns an evenly distributed random integer `at_least <= i <= at_most`. |
| 121 | /// See ::uintLessThan, which this function uses in most cases, | 189 | /// See ::uintLessThan, which this function uses in most cases, |
| 122 | /// for commentary on the runtime of this function. | 190 | /// for commentary on the runtime of this function. |
| ... | @@ -135,15 +203,11 @@ pub const Random = struct { | ... | @@ -135,15 +203,11 @@ pub const Random = struct { |
| 135 | } | 203 | } |
| 136 | } | 204 | } |
| 137 | 205 | ||
| 138 | /// Return a random integer/boolean type. | ||
| 139 | /// TODO: deprecated. use ::boolean or ::int instead. | 206 | /// TODO: deprecated. use ::boolean or ::int instead. |
| 140 | pub fn scalar(r: *Random, comptime T: type) T { | 207 | pub fn scalar(r: *Random, comptime T: type) T { |
| 141 | if (T == bool) return r.boolean(); | 208 | return if (T == bool) r.boolean() else r.int(T); |
| 142 | return r.int(T); | ||
| 143 | } | 209 | } |
| 144 | 210 | ||
| 145 | /// Return a random integer with even distribution between `start` | ||
| 146 | /// inclusive and `end` exclusive. `start` must be less than `end`. | ||
| 147 | /// TODO: deprecated. renamed to ::intRangeLessThan | 211 | /// TODO: deprecated. renamed to ::intRangeLessThan |
| 148 | pub fn range(r: *Random, comptime T: type, start: T, end: T) T { | 212 | pub fn range(r: *Random, comptime T: type, start: T, end: T) T { |
| 149 | return r.intRangeLessThan(T, start, end); | 213 | return r.intRangeLessThan(T, start, end); |
| ... | @@ -206,6 +270,20 @@ pub const Random = struct { | ... | @@ -206,6 +270,20 @@ pub const Random = struct { |
| 206 | } | 270 | } |
| 207 | }; | 271 | }; |
| 208 | 272 | ||
| 273 | /// Convert a random integer 0 <= random_int <= maxValue(T), | ||
| 274 | /// into an integer 0 <= result < less_than. | ||
| 275 | /// This function introduces a minor bias. | ||
| 276 | pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T { | ||
| 277 | comptime assert(T.is_signed == false); | ||
| 278 | const T2 = @IntType(false, T.bit_count * 2); | ||
| 279 | |||
| 280 | // adapted from: | ||
| 281 | // http://www.pcg-random.org/posts/bounded-rands.html | ||
| 282 | // "Integer Multiplication (Biased)" | ||
| 283 | var m: T2 = T2(random_int) * T2(less_than); | ||
| 284 | return @intCast(T, m >> T.bit_count); | ||
| 285 | } | ||
| 286 | |||
| 209 | const SequentialPrng = struct { | 287 | const SequentialPrng = struct { |
| 210 | const Self = @This(); | 288 | const Self = @This(); |
| 211 | random: Random, | 289 | random: Random, |
| ... | @@ -294,10 +372,19 @@ fn testRandomIntLessThan() void { | ... | @@ -294,10 +372,19 @@ fn testRandomIntLessThan() void { |
| 294 | var r = SequentialPrng.init(); | 372 | var r = SequentialPrng.init(); |
| 295 | r.next_value = 0xff; | 373 | r.next_value = 0xff; |
| 296 | assert(r.random.uintLessThan(u8, 4) == 3); | 374 | assert(r.random.uintLessThan(u8, 4) == 3); |
| 297 | r.next_value = 0xff; | 375 | assert(r.next_value == 0); |
| 298 | assert(r.random.uintLessThan(u8, 3) == 0); | 376 | assert(r.random.uintLessThan(u8, 4) == 0); |
| 299 | assert(r.next_value == 1); | 377 | assert(r.next_value == 1); |
| 300 | 378 | ||
| 379 | r.next_value = 0; | ||
| 380 | assert(r.random.uintLessThan(u64, 32) == 0); | ||
| 381 | |||
| 382 | // trigger the bias rejection code path | ||
| 383 | r.next_value = 0; | ||
| 384 | assert(r.random.uintLessThan(u8, 3) == 0); | ||
| 385 | // verify we incremented twice | ||
| 386 | assert(r.next_value == 2); | ||
| 387 | |||
| 301 | r.next_value = 0xff; | 388 | r.next_value = 0xff; |
| 302 | assert(r.random.intRangeLessThan(u8, 0, 0x80) == 0x7f); | 389 | assert(r.random.intRangeLessThan(u8, 0, 0x80) == 0x7f); |
| 303 | r.next_value = 0xff; | 390 | r.next_value = 0xff; |
| ... | @@ -310,17 +397,10 @@ fn testRandomIntLessThan() void { | ... | @@ -310,17 +397,10 @@ fn testRandomIntLessThan() void { |
| 310 | r.next_value = 0xff; | 397 | r.next_value = 0xff; |
| 311 | assert(r.random.intRangeLessThan(i8, -0x80, 0) == -1); | 398 | assert(r.random.intRangeLessThan(i8, -0x80, 0) == -1); |
| 312 | 399 | ||
| 313 | r.next_value = 0xff; | ||
| 314 | assert(r.random.intRangeLessThan(i64, -0x8000000000000000, 0) == -1); | ||
| 315 | r.next_value = 0xff; | 400 | r.next_value = 0xff; |
| 316 | assert(r.random.intRangeLessThan(i3, -4, 0) == -1); | 401 | assert(r.random.intRangeLessThan(i3, -4, 0) == -1); |
| 317 | r.next_value = 0xff; | 402 | r.next_value = 0xff; |
| 318 | assert(r.random.intRangeLessThan(i3, -2, 2) == 1); | 403 | assert(r.random.intRangeLessThan(i3, -2, 2) == 1); |
| 319 | |||
| 320 | // test retrying and eventually getting a good value | ||
| 321 | // start just out of bounds | ||
| 322 | r.next_value = 0x81; | ||
| 323 | assert(r.random.uintLessThan(u8, 0x81) == 0); | ||
| 324 | } | 404 | } |
| 325 | 405 | ||
| 326 | test "Random intAtMost" { | 406 | test "Random intAtMost" { |
| ... | @@ -332,9 +412,14 @@ fn testRandomIntAtMost() void { | ... | @@ -332,9 +412,14 @@ fn testRandomIntAtMost() void { |
| 332 | var r = SequentialPrng.init(); | 412 | var r = SequentialPrng.init(); |
| 333 | r.next_value = 0xff; | 413 | r.next_value = 0xff; |
| 334 | assert(r.random.uintAtMost(u8, 3) == 3); | 414 | assert(r.random.uintAtMost(u8, 3) == 3); |
| 335 | r.next_value = 0xff; | 415 | assert(r.next_value == 0); |
| 416 | assert(r.random.uintAtMost(u8, 3) == 0); | ||
| 417 | |||
| 418 | // trigger the bias rejection code path | ||
| 419 | r.next_value = 0; | ||
| 336 | assert(r.random.uintAtMost(u8, 2) == 0); | 420 | assert(r.random.uintAtMost(u8, 2) == 0); |
| 337 | assert(r.next_value == 1); | 421 | // verify we incremented twice |
| 422 | assert(r.next_value == 2); | ||
| 338 | 423 | ||
| 339 | r.next_value = 0xff; | 424 | r.next_value = 0xff; |
| 340 | assert(r.random.intRangeAtMost(u8, 0, 0x7f) == 0x7f); | 425 | assert(r.random.intRangeAtMost(u8, 0, 0x7f) == 0x7f); |
| ... | @@ -348,17 +433,43 @@ fn testRandomIntAtMost() void { | ... | @@ -348,17 +433,43 @@ fn testRandomIntAtMost() void { |
| 348 | r.next_value = 0xff; | 433 | r.next_value = 0xff; |
| 349 | assert(r.random.intRangeAtMost(i8, -0x80, -1) == -1); | 434 | assert(r.random.intRangeAtMost(i8, -0x80, -1) == -1); |
| 350 | 435 | ||
| 351 | r.next_value = 0xff; | ||
| 352 | assert(r.random.intRangeAtMost(i64, -0x8000000000000000, -1) == -1); | ||
| 353 | r.next_value = 0xff; | 436 | r.next_value = 0xff; |
| 354 | assert(r.random.intRangeAtMost(i3, -4, -1) == -1); | 437 | assert(r.random.intRangeAtMost(i3, -4, -1) == -1); |
| 355 | r.next_value = 0xff; | 438 | r.next_value = 0xff; |
| 356 | assert(r.random.intRangeAtMost(i3, -2, 1) == 1); | 439 | assert(r.random.intRangeAtMost(i3, -2, 1) == 1); |
| 357 | 440 | ||
| 358 | // test retrying and eventually getting a good value | 441 | assert(r.random.uintAtMost(u0, 0) == 0); |
| 359 | // start just out of bounds | 442 | } |
| 360 | r.next_value = 0x81; | 443 | |
| 361 | assert(r.random.uintAtMost(u8, 0x80) == 0); | 444 | test "Random Biased" { |
| 445 | var r = DefaultPrng.init(0); | ||
| 446 | // Not thoroughly checking the logic here. | ||
| 447 | // Just want to execute all the paths with different types. | ||
| 448 | |||
| 449 | assert(r.random.uintLessThanBiased(u1, 1) == 0); | ||
| 450 | assert(r.random.uintLessThanBiased(u32, 10) < 10); | ||
| 451 | assert(r.random.uintLessThanBiased(u64, 20) < 20); | ||
| 452 | |||
| 453 | assert(r.random.uintAtMostBiased(u0, 0) == 0); | ||
| 454 | assert(r.random.uintAtMostBiased(u1, 0) <= 0); | ||
| 455 | assert(r.random.uintAtMostBiased(u32, 10) <= 10); | ||
| 456 | assert(r.random.uintAtMostBiased(u64, 20) <= 20); | ||
| 457 | |||
| 458 | assert(r.random.intRangeLessThanBiased(u1, 0, 1) == 0); | ||
| 459 | assert(r.random.intRangeLessThanBiased(i1, -1, 0) == -1); | ||
| 460 | assert(r.random.intRangeLessThanBiased(u32, 10, 20) >= 10); | ||
| 461 | assert(r.random.intRangeLessThanBiased(i32, 10, 20) >= 10); | ||
| 462 | assert(r.random.intRangeLessThanBiased(u64, 20, 40) >= 20); | ||
| 463 | assert(r.random.intRangeLessThanBiased(i64, 20, 40) >= 20); | ||
| 464 | |||
| 465 | // uncomment for broken module error: | ||
| 466 | //assert(r.random.intRangeAtMostBiased(u0, 0, 0) == 0); | ||
| 467 | assert(r.random.intRangeAtMostBiased(u1, 0, 1) >= 0); | ||
| 468 | assert(r.random.intRangeAtMostBiased(i1, -1, 0) >= -1); | ||
| 469 | assert(r.random.intRangeAtMostBiased(u32, 10, 20) >= 10); | ||
| 470 | assert(r.random.intRangeAtMostBiased(i32, 10, 20) >= 10); | ||
| 471 | assert(r.random.intRangeAtMostBiased(u64, 20, 40) >= 20); | ||
| 472 | assert(r.random.intRangeAtMostBiased(i64, 20, 40) >= 20); | ||
| 362 | } | 473 | } |
| 363 | 474 | ||
| 364 | // Generator to extend 64-bit seed values into longer sequences. | 475 | // Generator to extend 64-bit seed values into longer sequences. |
| ... | @@ -870,12 +981,16 @@ test "Random range" { | ... | @@ -870,12 +981,16 @@ test "Random range" { |
| 870 | } | 981 | } |
| 871 | 982 | ||
| 872 | fn testRange(r: *Random, start: i8, end: i8) void { | 983 | fn testRange(r: *Random, start: i8, end: i8) void { |
| 984 | testRangeBias(r, start, end, true); | ||
| 985 | testRangeBias(r, start, end, false); | ||
| 986 | } | ||
| 987 | fn testRangeBias(r: *Random, start: i8, end: i8, biased: bool) void { | ||
| 873 | const count = @intCast(usize, i32(end) - i32(start)); | 988 | const count = @intCast(usize, i32(end) - i32(start)); |
| 874 | var values_buffer = []bool{false} ** 0x100; | 989 | var values_buffer = []bool{false} ** 0x100; |
| 875 | const values = values_buffer[0..count]; | 990 | const values = values_buffer[0..count]; |
| 876 | var i: usize = 0; | 991 | var i: usize = 0; |
| 877 | while (i < count) { | 992 | while (i < count) { |
| 878 | const value: i32 = r.intRangeLessThan(i8, start, end); | 993 | const value: i32 = if (biased) r.intRangeLessThanBiased(i8, start, end) else r.intRangeLessThan(i8, start, end); |
| 879 | const index = @intCast(usize, value - start); | 994 | const index = @intCast(usize, value - start); |
| 880 | if (!values[index]) { | 995 | if (!values[index]) { |
| 881 | i += 1; | 996 | i += 1; |
test/runtime_safety.zig+10| ... | @@ -275,6 +275,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { | ... | @@ -275,6 +275,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 275 | \\} | 275 | \\} |
| 276 | ); | 276 | ); |
| 277 | 277 | ||
| 278 | cases.addRuntimeSafety("signed integer not fitting in cast to unsigned integer - widening", | ||
| 279 | \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { | ||
| 280 | \\ @import("std").os.exit(126); | ||
| 281 | \\} | ||
| 282 | \\pub fn main() void { | ||
| 283 | \\ var value: c_short = -1; | ||
| 284 | \\ var casted = @intCast(u32, value); | ||
| 285 | \\} | ||
| 286 | ); | ||
| 287 | |||
| 278 | cases.addRuntimeSafety("unwrap error", | 288 | cases.addRuntimeSafety("unwrap error", |
| 279 | \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { | 289 | \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { |
| 280 | \\ if (@import("std").mem.eql(u8, message, "attempt to unwrap error: Whatever")) { | 290 | \\ if (@import("std").mem.eql(u8, message, "attempt to unwrap error: Whatever")) { |