Skip to content

Commit abd0a83

Browse files
committed
Split up public/internal options
1 parent d2ec362 commit abd0a83

File tree

6 files changed

+88
-75
lines changed

6 files changed

+88
-75
lines changed

include/prism/internal/options.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @file internal/options.h
3+
*
4+
* The options that can be passed to parsing.
5+
*/
6+
#ifndef PRISM_INTERNAL_OPTIONS_H
7+
#define PRISM_INTERNAL_OPTIONS_H
8+
9+
#include "prism/options.h"
10+
11+
/**
12+
* Deserialize an options struct from the given binary string. This is used to
13+
* pass options to the parser from an FFI call so that consumers of the library
14+
* from an FFI perspective don't have to worry about the structure of our
15+
* options structs. Since the source of these calls will be from Ruby
16+
* implementation internals we assume it is from a trusted source.
17+
*
18+
* `data` is assumed to be a valid pointer pointing to well-formed data. The
19+
* layout of this data should be the same every time, and is described below:
20+
*
21+
* | # bytes | field |
22+
* | ------- | -------------------------- |
23+
* | `4` | the length of the filepath |
24+
* | ... | the filepath bytes |
25+
* | `4` | the line number |
26+
* | `4` | the length the encoding |
27+
* | ... | the encoding bytes |
28+
* | `1` | frozen string literal |
29+
* | `1` | -p command line option |
30+
* | `1` | -n command line option |
31+
* | `1` | -l command line option |
32+
* | `1` | -a command line option |
33+
* | `1` | the version |
34+
* | `1` | encoding locked |
35+
* | `1` | main script |
36+
* | `1` | partial script |
37+
* | `1` | freeze |
38+
* | `4` | the number of scopes |
39+
* | ... | the scopes |
40+
*
41+
* The version field is an enum, so it should be one of the following values:
42+
*
43+
* | value | version |
44+
* | ----- | ------------------------- |
45+
* | `0` | use the latest version of prism |
46+
* | `1` | use the version of prism that is vendored in CRuby 3.3.0 |
47+
* | `2` | use the version of prism that is vendored in CRuby 3.4.0 |
48+
* | `3` | use the version of prism that is vendored in CRuby 4.0.0 |
49+
* | `4` | use the version of prism that is vendored in CRuby 4.1.0 |
50+
*
51+
* Each scope is laid out as follows:
52+
*
53+
* | # bytes | field |
54+
* | ------- | -------------------------- |
55+
* | `4` | the number of locals |
56+
* | `1` | the forwarding flags |
57+
* | ... | the locals |
58+
*
59+
* Each local is laid out as follows:
60+
*
61+
* | # bytes | field |
62+
* | ------- | -------------------------- |
63+
* | `4` | the length of the local |
64+
* | ... | the local bytes |
65+
*
66+
* Some additional things to note about this layout:
67+
*
68+
* * The filepath can have a length of 0, in which case we'll consider it an
69+
* empty string.
70+
* * The line number should be 0-indexed.
71+
* * The encoding can have a length of 0, in which case we'll use the default
72+
* encoding (UTF-8). If it's not 0, it should correspond to a name of an
73+
* encoding that can be passed to `Encoding.find` in Ruby.
74+
* * The frozen string literal, encoding locked, main script, and partial script
75+
* fields are booleans, so their values should be either 0 or 1.
76+
* * The number of scopes can be 0.
77+
*
78+
* @param options The options struct to deserialize into.
79+
* @param data The binary string to deserialize from.
80+
*/
81+
void pm_options_read(pm_options_t *options, const char *data);
82+
83+
#endif

include/prism/options.h

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
#ifndef PRISM_OPTIONS_H
77
#define PRISM_OPTIONS_H
88

9-
#include "prism/defines.h"
109
#include "prism/strings.h"
1110

1211
#include <stdbool.h>
1312
#include <stddef.h>
14-
#include <stdint.h>
1513

1614
/**
1715
* String literals should be made frozen.
@@ -58,7 +56,7 @@ static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK = 0x4;
5856
/** When the scope is fowarding with the ... parameter. */
5957
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL = 0x8;
6058

61-
// Forward declaration needed by the callback typedef.
59+
/* Forward declaration needed by the callback typedef. */
6260
struct pm_options;
6361

6462
/**
@@ -418,76 +416,4 @@ PRISM_EXPORTED_FUNCTION void pm_options_scope_forwarding_set(pm_options_scope_t
418416
*/
419417
PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
420418

421-
/**
422-
* Deserialize an options struct from the given binary string. This is used to
423-
* pass options to the parser from an FFI call so that consumers of the library
424-
* from an FFI perspective don't have to worry about the structure of our
425-
* options structs. Since the source of these calls will be from Ruby
426-
* implementation internals we assume it is from a trusted source.
427-
*
428-
* `data` is assumed to be a valid pointer pointing to well-formed data. The
429-
* layout of this data should be the same every time, and is described below:
430-
*
431-
* | # bytes | field |
432-
* | ------- | -------------------------- |
433-
* | `4` | the length of the filepath |
434-
* | ... | the filepath bytes |
435-
* | `4` | the line number |
436-
* | `4` | the length the encoding |
437-
* | ... | the encoding bytes |
438-
* | `1` | frozen string literal |
439-
* | `1` | -p command line option |
440-
* | `1` | -n command line option |
441-
* | `1` | -l command line option |
442-
* | `1` | -a command line option |
443-
* | `1` | the version |
444-
* | `1` | encoding locked |
445-
* | `1` | main script |
446-
* | `1` | partial script |
447-
* | `1` | freeze |
448-
* | `4` | the number of scopes |
449-
* | ... | the scopes |
450-
*
451-
* The version field is an enum, so it should be one of the following values:
452-
*
453-
* | value | version |
454-
* | ----- | ------------------------- |
455-
* | `0` | use the latest version of prism |
456-
* | `1` | use the version of prism that is vendored in CRuby 3.3.0 |
457-
* | `2` | use the version of prism that is vendored in CRuby 3.4.0 |
458-
* | `3` | use the version of prism that is vendored in CRuby 4.0.0 |
459-
* | `4` | use the version of prism that is vendored in CRuby 4.1.0 |
460-
*
461-
* Each scope is laid out as follows:
462-
*
463-
* | # bytes | field |
464-
* | ------- | -------------------------- |
465-
* | `4` | the number of locals |
466-
* | `1` | the forwarding flags |
467-
* | ... | the locals |
468-
*
469-
* Each local is laid out as follows:
470-
*
471-
* | # bytes | field |
472-
* | ------- | -------------------------- |
473-
* | `4` | the length of the local |
474-
* | ... | the local bytes |
475-
*
476-
* Some additional things to note about this layout:
477-
*
478-
* * The filepath can have a length of 0, in which case we'll consider it an
479-
* empty string.
480-
* * The line number should be 0-indexed.
481-
* * The encoding can have a length of 0, in which case we'll use the default
482-
* encoding (UTF-8). If it's not 0, it should correspond to a name of an
483-
* encoding that can be passed to `Encoding.find` in Ruby.
484-
* * The frozen string literal, encoding locked, main script, and partial script
485-
* fields are booleans, so their values should be either 0 or 1.
486-
* * The number of scopes can be 0.
487-
*
488-
* @param options The options struct to deserialize into.
489-
* @param data The binary string to deserialize from.
490-
*/
491-
void pm_options_read(pm_options_t *options, const char *data);
492-
493419
#endif

prism.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Gem::Specification.new do |spec|
8686
"include/prism/internal/line_offset_list.h",
8787
"include/prism/internal/list.h",
8888
"include/prism/internal/memchr.h",
89+
"include/prism/internal/options.h",
8990
"include/prism/internal/static_literals.h",
9091
"include/prism/internal/strncasecmp.h",
9192
"include/prism/internal/strings.h",

src/options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "prism/allocator.h"
55

66
#include <stdlib.h>
7+
#include <string.h>
78

89
/**
910
* Set the shebang callback option on the given options struct.

src/prism.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "prism/internal/integer.h"
1010
#include "prism/internal/line_offset_list.h"
1111
#include "prism/internal/list.h"
12+
#include "prism/internal/options.h"
1213
#include "prism/internal/static_literals.h"
1314
#include "prism/internal/strings.h"
1415
#include "prism/internal/strncasecmp.h"

templates/src/serialize.c.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "prism.h"
22

33
#include "prism/internal/list.h"
4+
#include "prism/internal/options.h"
45

56
// We optionally support serializing to a binary string. For systems that don't
67
// want or need this functionality, it can be turned off with the

0 commit comments

Comments
 (0)