1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use std::{fmt, ops, slice, str};
use std::fmt::{Display, Formatter};
use std::os::raw::{c_char, c_void};

use libc::{ptrdiff_t, size_t};

use crate::bridge::{FfiapiDefine, FfiapiEngine, FfiapiLoader, GoString};

// We wrap C arrays allocated from Go and sent to us in SliceContainer, such as `*ffiapi_message`.
// This will own the memory, make it usable as a slice, and drop using the matching deallocator.
pub struct SliceContainer<T> {
    pub(crate) ptr: *mut T,
    pub(crate) len: usize,
}

impl<T> ops::Deref for SliceContainer<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        unsafe {
            slice::from_raw_parts(self.ptr, self.len)
        }
    }
}

impl<T> Drop for SliceContainer<T> {
    fn drop(&mut self) {
        unsafe {
            // We pass `malloc` to Go as the allocator.
            libc::free(self.ptr as *mut c_void);
        };
    }
}

// This is the ffiapi_string struct in C; we declare it here to avoid having to needlessly rewrap in StrContainer.
// This will own the memory, make it usable as a str, and drop using the matching deallocator.
#[repr(C)]
pub struct StrContainer {
    len: size_t,
    data: *mut c_char,
}

impl ops::Deref for StrContainer {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        unsafe {
            str::from_utf8_unchecked(slice::from_raw_parts(self.data as *mut u8, self.len))
        }
    }
}

impl Drop for StrContainer {
    fn drop(&mut self) {
        unsafe {
            // We pass `malloc` to Go as the allocator.
            libc::free(self.data as *mut c_void);
        };
    }
}

// This is the ffiapi_message struct in C; we declare it here to avoid having to needlessly rewrap in Message.
#[repr(C)]
pub struct Message {
    pub file: StrContainer,
    pub line: ptrdiff_t,
    pub column: ptrdiff_t,
    pub length: ptrdiff_t,
    pub text: StrContainer,
}

impl Display for Message {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{} [{}:{}:{}]", &*self.text, &*self.file, self.line, self.column)
    }
}

// This is the ffiapi_output_file struct in C; we declare it here to avoid having to needlessly rewrap in OutputFile.
#[repr(C)]
pub struct OutputFile {
    pub path: StrContainer,
    pub data: StrContainer,
}

#[derive(Copy, Clone)]
pub enum EngineName {
    Chrome,
    Edge,
    Firefox,
    IOS,
    Node,
    Safari,
}

#[derive(Copy, Clone)]
pub enum Format {
    Default,
    IIFE,
    CommonJS,
    ESModule,
}

#[derive(Copy, Clone)]
pub enum Loader {
    JS,
    JSX,
    TS,
    TSX,
    JSON,
    Text,
    Base64,
    DataURL,
    File,
    Binary,
}

#[derive(Copy, Clone)]
pub enum Platform {
    Browser,
    Node,
}

#[derive(Copy, Clone)]
pub enum SourceMap {
    None,
    Inline,
    Linked,
    External,
}

#[derive(Copy, Clone)]
pub enum Target {
    ESNext,
    ES5,
    ES2015,
    ES2016,
    ES2017,
    ES2018,
    ES2019,
    ES2020,
}

pub struct Defines {
    pub(crate) vec: Vec<FfiapiDefine>,
}

impl Defines {
    pub fn new() -> Defines {
        Defines {
            vec: Vec::new(),
        }
    }

    pub fn add(&mut self, name: String, value: String) -> () {
        self.vec.push(FfiapiDefine {
            from: GoString::from_string(name),
            to: GoString::from_string(value),
        });
    }
}

pub struct Engines {
    pub(crate) vec: Vec<FfiapiEngine>,
}

impl Engines {
    pub fn new() -> Engines {
        Engines {
            vec: Vec::new(),
        }
    }

    pub fn add(&mut self, name: EngineName, version: String) -> () {
        self.vec.push(FfiapiEngine {
            name: name as u8,
            version: GoString::from_string(version),
        });
    }
}

pub struct EntryPoints {
    pub(crate) vec: Vec<GoString>,
}

impl EntryPoints {
    pub fn new() -> EntryPoints {
        EntryPoints {
            vec: Vec::new(),
        }
    }

    pub fn add(&mut self, name: String) -> () {
        self.vec.push(GoString::from_string(name));
    }
}

pub struct Externals {
    pub(crate) vec: Vec<GoString>,
}

impl Externals {
    pub fn new() -> Externals {
        Externals {
            vec: Vec::new(),
        }
    }

    pub fn add(&mut self, name: String) -> () {
        self.vec.push(GoString::from_string(name));
    }
}

pub struct Loaders {
    pub(crate) vec: Vec<FfiapiLoader>,
}

impl Loaders {
    pub fn new() -> Loaders {
        Loaders {
            vec: Vec::new(),
        }
    }

    pub fn add(&mut self, name: String, loader: Loader) -> () {
        self.vec.push(FfiapiLoader {
            name: GoString::from_string(name),
            loader: loader as u8,
        });
    }
}

pub struct PureFunctions {
    pub(crate) vec: Vec<GoString>,
}

impl PureFunctions {
    pub fn new() -> PureFunctions {
        PureFunctions {
            vec: Vec::new(),
        }
    }

    pub fn add(&mut self, name: String) -> () {
        self.vec.push(GoString::from_string(name));
    }
}

pub struct ResolveExtensions {
    pub(crate) vec: Vec<GoString>,
}

impl ResolveExtensions {
    pub fn new() -> ResolveExtensions {
        ResolveExtensions {
            vec: Vec::new(),
        }
    }

    pub fn add(&mut self, ext: String) -> () {
        self.vec.push(GoString::from_string(ext));
    }
}

pub struct StrictOptions {
    pub nullish_coalescing: bool,
    pub class_fields: bool,
}

pub struct BuildOptions {
    pub source_map: SourceMap,
    pub target: Target,
    pub engines: Engines,
    pub strict: StrictOptions,

    pub minify_whitespace: bool,
    pub minify_identifiers: bool,
    pub minify_syntax: bool,

    pub jsx_factory: String,
    pub jsx_fragment: String,

    pub defines: Defines,
    pub pure_functions: PureFunctions,

    pub global_name: String,
    pub bundle: bool,
    pub splitting: bool,
    pub outfile: String,
    pub metafile: String,
    pub outdir: String,
    pub platform: Platform,
    pub format: Format,
    pub externals: Externals,
    pub loaders: Loaders,
    pub resolve_extensions: ResolveExtensions,
    pub tsconfig: String,

    pub entry_points: EntryPoints,
}

pub struct BuildResult {
    pub output_files: SliceContainer<OutputFile>,
    pub errors: SliceContainer<Message>,
    pub warnings: SliceContainer<Message>,
}

pub struct TransformOptions {
    pub source_map: SourceMap,
    pub target: Target,
    pub engines: Engines,
    pub strict: StrictOptions,

    pub minify_whitespace: bool,
    pub minify_identifiers: bool,
    pub minify_syntax: bool,

    pub jsx_factory: String,
    pub jsx_fragment: String,

    pub defines: Defines,
    pub pure_functions: PureFunctions,

    pub source_file: String,
    pub loader: Loader,
}

pub struct TransformResult {
    pub js: StrContainer,
    pub js_source_map: StrContainer,
    pub errors: SliceContainer<Message>,
    pub warnings: SliceContainer<Message>,
}