-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglue.html
657 lines (636 loc) · 49.5 KB
/
glue.html
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title> glue - everyday Lua functions </title>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script src="jquery-tablesorter.js"></script>
<script src="strftime.js"></script>
<script src="mustache.js"></script>
<script src="config.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" type="text/css" href="templates/reset.css" />
<link rel="stylesheet" type="text/css" href="templates/hack.css" />
<link rel="stylesheet" type="text/css" id="lights_css" />
<script>
var DOCNAME = 'glue'
var PROJECT = 'glue'
//set the lights before rendering starts
set_lights()
</script>
</head>
<body>
<header>
<div class="container">
<div class="btn-container btn-lights-container">
<a href="#" class="btn btn-lights" id="lights">lights</a>
</div>
<div class="btn-container btn-home-container">
<a href="/" class="btn btn-home">luapower</a>
</div>
<table id="header_table">
<tr>
<td style="vertical-align: middle;" width="100%">
<h1> glue </h1>
<h2> everyday Lua functions </h2>
</td>
<td style="vertical-align: middle;" align="right" style="height: 150px">
<table><tr><td>
<div class="doc" id="package_info_container">
<div id="package_info"> </div>
<div id="commit_log"> </div>
</div>
<a href="https://github.com/luapower/glue" class="btn btn-rightside btn-github"><span class="icon"></span>View on GitHub</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/glue/tarball/master" class="btn btn-rightside">Download as .tar.gz</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/glue/zipball/master" class="btn btn-rightside">Download as .zip</a>
</td></tr></table>
</td>
</tr>
</table>
<div class="btn-container btn-discuss-container">
<a href="https://github.com/luapower/glue/issues/new" target="_blank"
class="btn btn-rightside btn-discuss"><span class="icon"></span>Discuss</a>
</div>
</div>
</header>
<div class="bg-container">
<div class="bg-center-container">
<div class="bg bg-glue" style="background-image: url('bg/glue.png');"></div>
</div>
</div>
<div class="under-header">
<div class="container">
<div id="toc_container" class="toc_container doc"></div>
<button id=tab1_button class="tab_button hidden">Documentation</button>
<button id=tab2_button class="tab_button hidden">Package Info</button>
<div id="tabs_cointainer">
<div id="tab1_container">
<section class="doc">
<span id="module_info"></span>
<h2 id="local-glue-requireglue"><code>local glue = require'glue'</code></h2>
<table>
<tbody>
<tr class="odd">
<td align="left"><strong>tables</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">glue.index(t) -> dt</td>
<td align="left"><a href="#index">switch keys with values</a></td>
</tr>
<tr class="odd">
<td align="left">glue.keys(t[, sorted | cmp]) -> dt</td>
<td align="left"><a href="#keys">make a list of all the keys</a></td>
</tr>
<tr class="even">
<td align="left">glue.update(dt,t1,...) -> dt</td>
<td align="left"><a href="#update">merge tables - overwrites keys</a></td>
</tr>
<tr class="odd">
<td align="left">glue.merge(dt,t1,...) -> dt</td>
<td align="left"><a href="#merge">merge tables - no overwriting</a></td>
</tr>
<tr class="even">
<td align="left">glue.sortedpairs(t[, cmp])-> iterator<k,v></td>
<td align="left"><a href="#sortedpairs">like pairs() but in key order</a></td>
</tr>
<tr class="odd">
<td align="left"><strong>lists</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">glue.extend(dt,t1,...) -> dt</td>
<td align="left"><a href="#extend">extend a list</a></td>
</tr>
<tr class="odd">
<td align="left">glue.append(dt,v1,...) -> dt</td>
<td align="left"><a href="#append">append non-nil values to a list</a></td>
</tr>
<tr class="even">
<td align="left">glue.shift(t,i,n) -> t</td>
<td align="left"><a href="#shift">shift list elements</a></td>
</tr>
<tr class="odd">
<td align="left"><strong>strings</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">glue.gsplit(s,sep[, plain]) -> iterator<e[,captures...]></td>
<td align="left"><a href="#gsplit">split a string by a pattern</a></td>
</tr>
<tr class="odd">
<td align="left">glue.trim(s) -> s</td>
<td align="left"><a href="#trim">remove padding</a></td>
</tr>
<tr class="even">
<td align="left">glue.escape(s[,mode])-> s</td>
<td align="left"><a href="#escape">escape magic pattern characters</a></td>
</tr>
<tr class="odd">
<td align="left">glue.tohex(s) -> s</td>
<td align="left"><a href="#tohex">string to hex</a></td>
</tr>
<tr class="even">
<td align="left">glue.fromhex(s) -> s</td>
<td align="left"><a href="#fromhex">hex to string</a></td>
</tr>
<tr class="odd">
<td align="left"><strong>iterators</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">glue.collect([i,]iterator)-> t</td>
<td align="left"><a href="#collect">collect iterated values into a list</a></td>
</tr>
<tr class="odd">
<td align="left"><strong>closures</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">glue.pass(...) -> ...</td>
<td align="left"><a href="#pass">does nothing, returns back all arguments</a></td>
</tr>
<tr class="odd">
<td align="left"><strong>metatables</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">glue.inherit(t,parent) -> t</td>
<td align="left"><a href="#inherit">set or clear inheritance</a></td>
</tr>
<tr class="odd">
<td align="left">glue.autotable([t]) -> t</td>
<td align="left"><a href="#autotable">autotable pattern</a></td>
</tr>
<tr class="even">
<td align="left"><strong>i/o</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">glue.fileexists(file) -> true | false</td>
<td align="left"><a href="#fileexists">check if a file exists and it's readable</a></td>
</tr>
<tr class="even">
<td align="left">glue.readfile(file[,format]) -> s | nil, err</td>
<td align="left"><a href="#readfile">read the contents of a file into a string</a></td>
</tr>
<tr class="odd">
<td align="left">glue.writefile(file,s[,format])</td>
<td align="left"><a href="#writefile">write a string to a file</a></td>
</tr>
<tr class="even">
<td align="left"><strong>errors</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">glue.assert(v,[message[,args...]]) -> args</td>
<td align="left"><a href="#assert">assert with error message formatting</a></td>
</tr>
<tr class="even">
<td align="left">glue.unprotect(ok,result,...) -> result,... | nil,result,...</td>
<td align="left"><a href="#unprotect">unprotect a protected call</a></td>
</tr>
<tr class="odd">
<td align="left">glue.pcall(f,...) -> true,... | false,traceback</td>
<td align="left"><a href="#pcall">pcall with traceback</a> <em>(not for Lua 5.1)</em></td>
</tr>
<tr class="even">
<td align="left">glue.fpcall(f,...) -> result | nil,traceback</td>
<td align="left"><a href="#fpcall">coding with finally and except</a></td>
</tr>
<tr class="odd">
<td align="left">glue.fcall(f,...) -> result</td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><strong>modules</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">glue.autoload(t, submodule) -> t</td>
<td align="left"><a href="#autoload">autoload table keys from submodules</a></td>
</tr>
<tr class="even">
<td align="left">glue.autoload(t, key, module|loader) -> t</td>
<td align="left"><a href="#autoload">autoload table keys from submodules</a></td>
</tr>
<tr class="odd">
<td align="left">glue.bin</td>
<td align="left"><a href="#bin">get the script's directory</a></td>
</tr>
<tr class="even">
<td align="left">glue.luapath(path[, index[, ext]])</td>
<td align="left"><a href="#luapath">insert a path in package.path</a></td>
</tr>
<tr class="odd">
<td align="left">glue.cpath(path[, index])</td>
<td align="left"><a href="#cpath">insert a path in package.cpath</a></td>
</tr>
<tr class="even">
<td align="left"><strong>malloc</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">glue.malloc([ctype, ]size) -> cdata</td>
<td align="left"><a href="#malloc-array">allocate an array using system's malloc</a></td>
</tr>
<tr class="even">
<td align="left">glue.malloc(ctype) -> cdata</td>
<td align="left"><a href="#malloc-ctype">allocate a C type using system's malloc</a></td>
</tr>
<tr class="odd">
<td align="left">glue.free(cdata)</td>
<td align="left"><a href="#free">free malloc'ed memory</a></td>
</tr>
</tbody>
</table>
<h2 id="glue.indext---dt"><code id="index">glue.index(t) -> dt</code></h2>
<p>Switch table keys with values.</p>
<h3 id="examples">Examples</h3>
<p>Extract a rfc850 date from a string. Use lookup tables for weekdays and months.</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">weekdays</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>index<span class="ot">{</span><span class="st">'Sunday'</span><span class="ot">,</span> <span class="st">'Monday'</span><span class="ot">,</span> <span class="st">'Tuesday'</span><span class="ot">,</span> <span class="st">'Wednesday'</span><span class="ot">,</span> <span class="st">'Thursday'</span><span class="ot">,</span> <span class="st">'Friday'</span><span class="ot">,</span> <span class="st">'Saturday'</span><span class="ot">}</span>
<span class="kw">local</span> <span class="kw">months</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>index<span class="ot">{</span><span class="st">'Jan'</span><span class="ot">,</span> <span class="st">'Feb'</span><span class="ot">,</span> <span class="st">'Mar'</span><span class="ot">,</span> <span class="st">'Apr'</span><span class="ot">,</span> <span class="st">'May'</span><span class="ot">,</span> <span class="st">'Jun'</span><span class="ot">,</span> <span class="st">'Jul'</span><span class="ot">,</span> <span class="st">'Aug'</span><span class="ot">,</span> <span class="st">'Sep'</span><span class="ot">,</span> <span class="st">'Oct'</span><span class="ot">,</span> <span class="st">'Nov'</span><span class="ot">,</span> <span class="st">'Dec'</span><span class="ot">}</span>
<span class="co">--weekday "," SP 2DIGIT "-" month "-" 2DIGIT SP 2DIGIT ":" 2DIGIT ":" 2DIGIT SP "GMT"</span>
<span class="co">--eg. Sunday, 06-Nov-94 08:49:37 GMT</span>
<span class="kw">function</span> rfc850date<span class="ot">(</span><span class="kw">s</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">w</span><span class="ot">,</span><span class="kw">d</span><span class="ot">,</span><span class="kw">mo</span><span class="ot">,</span><span class="kw">y</span><span class="ot">,</span><span class="kw">h</span><span class="ot">,</span><span class="kw">m</span><span class="ot">,</span><span class="kw">s</span> <span class="ot">=</span> <span class="kw">s</span>:match<span class="st">'([A-Za-z]+), (%d+)%-([A-Za-z]+)%-(%d+) (%d+):(%d+):(%d+) GMT'</span>
<span class="kw">d</span><span class="ot">,</span><span class="kw">y</span><span class="ot">,</span><span class="kw">h</span><span class="ot">,</span><span class="kw">m</span><span class="ot">,</span><span class="kw">s</span> <span class="ot">=</span> <span class="fu">tonumber</span><span class="ot">(</span><span class="kw">d</span><span class="ot">),</span><span class="fu">tonumber</span><span class="ot">(</span><span class="kw">y</span><span class="ot">),</span><span class="fu">tonumber</span><span class="ot">(</span><span class="kw">h</span><span class="ot">),</span><span class="fu">tonumber</span><span class="ot">(</span><span class="kw">m</span><span class="ot">),</span><span class="fu">tonumber</span><span class="ot">(</span><span class="kw">s</span><span class="ot">)</span>
<span class="kw">w</span> <span class="ot">=</span> <span class="fu">assert</span><span class="ot">(</span><span class="kw">weekdays</span><span class="ot">[</span><span class="kw">w</span><span class="ot">])</span>
<span class="kw">mo</span> <span class="ot">=</span> <span class="fu">assert</span><span class="ot">(</span><span class="kw">months</span><span class="ot">[</span><span class="kw">mo</span><span class="ot">])</span>
<span class="kw">if</span> <span class="kw">y</span> <span class="kw">then</span> <span class="kw">y</span> <span class="ot">=</span> <span class="kw">y</span> <span class="ot">+</span> <span class="ot">(</span><span class="kw">y</span> <span class="ot">></span> <span class="dv">50</span> <span class="kw">and</span> <span class="dv">1900</span> <span class="kw">or</span> <span class="dv">2000</span><span class="ot">)</span> <span class="kw">end</span>
<span class="kw">return</span> <span class="ot">{</span><span class="kw">wday</span> <span class="ot">=</span> <span class="kw">w</span><span class="ot">,</span> <span class="kw">day</span> <span class="ot">=</span> <span class="kw">d</span><span class="ot">,</span> <span class="kw">year</span> <span class="ot">=</span> <span class="kw">y</span><span class="ot">,</span> <span class="kw">month</span> <span class="ot">=</span> <span class="kw">mo</span><span class="ot">,</span> <span class="kw">hour</span> <span class="ot">=</span> <span class="kw">h</span><span class="ot">,</span> <span class="fu">min</span> <span class="ot">=</span> <span class="kw">m</span><span class="ot">,</span> <span class="kw">sec</span> <span class="ot">=</span> <span class="kw">s</span><span class="ot">}</span>
<span class="kw">end</span>
<span class="kw">for</span> <span class="kw">k</span><span class="ot">,</span><span class="kw">v</span> <span class="kw">in</span> <span class="fu">pairs</span><span class="ot">(</span>rfc850date<span class="st">'Sunday, 06-Nov-94 08:49:37 GMT'</span><span class="ot">)</span> <span class="kw">do</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">k</span><span class="ot">,</span><span class="kw">v</span><span class="ot">)</span>
<span class="kw">end</span></code></pre>
<p>Output</p>
<pre><code>day 6
sec 37
wday 1
min 49
year 1994
month 11
hour 8</code></pre>
<p>Copy-paste a bunch of defines from a C header file and create an inverse lookup table to find the name of a value at runtime.</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="co">--from ibase.h</span>
<span class="kw">info_end_codes</span> <span class="ot">=</span> <span class="ot">{</span>
<span class="kw">isc_info_end</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">,</span> <span class="co">--normal ending</span>
<span class="kw">isc_info_truncated</span> <span class="ot">=</span> <span class="dv">2</span><span class="ot">,</span> <span class="co">--receiving buffer too small</span>
<span class="kw">isc_info_error</span> <span class="ot">=</span> <span class="dv">3</span><span class="ot">,</span> <span class="co">--error, check status vector</span>
<span class="kw">isc_info_data_not_ready</span> <span class="ot">=</span> <span class="dv">4</span><span class="ot">,</span> <span class="co">--data not available for some reason</span>
<span class="kw">isc_info_svc_timeout</span> <span class="ot">=</span> <span class="dv">64</span><span class="ot">,</span> <span class="co">--timeout expired</span>
<span class="ot">}</span>
<span class="kw">info_end_code_names</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>index<span class="ot">(</span><span class="kw">info_end_codes</span><span class="ot">)</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">info_end_code_names</span><span class="ot">[</span><span class="dv">64</span><span class="ot">])</span></code></pre>
<p>Output</p>
<pre><code>isc_info_svc_timeout</code></pre>
<hr />
<h2 id="glue.keyst-sorted-cmp---dt"><code id="keys">glue.keys(t[, sorted | cmp]) -> dt</code></h2>
<p>Make a list of all the keys of <code>t</code>, optionally sorted.</p>
<h3 id="examples-1">Examples</h3>
<p>An API expects a list of things but you have them as keys in a table because you are indexing something on them.</p>
<p>For instance, you have a table of the form <code>{socket = thread}</code> but <code>socket.select</code> wants a list of sockets.</p>
<p>See also: <a href="#sortedpairs">glue.sortedpairs</a>.</p>
<hr />
<h2 id="glue.updatedtt1...---dt"><code id="update">glue.update(dt,t1,...) -> dt</code></h2>
<p>Update a table with elements of other tables, overwriting any existing keys.</p>
<ul>
<li>nil arguments are skipped.</li>
</ul>
<h3 id="examples-2">Examples</h3>
<p>Create an options table by merging the options received as an argument (if any) over the default options.</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">function</span> f<span class="ot">(</span><span class="kw">opts</span><span class="ot">)</span>
<span class="kw">opts</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>update<span class="ot">({},</span> <span class="kw">default_opts</span><span class="ot">,</span> <span class="kw">opts</span><span class="ot">)</span>
<span class="kw">end</span></code></pre>
<p>Shallow table copy:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">t</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>update<span class="ot">({},</span> <span class="kw">t</span><span class="ot">)</span></code></pre>
<p>Static multiple inheritance:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">C</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>update<span class="ot">({},</span> <span class="kw">A</span><span class="ot">,</span> <span class="kw">B</span><span class="ot">)</span> <span class="co">--#TODO: find real-world example of multiple inheritance</span></code></pre>
<p>See also: <a href="#extend">glue.extend</a>, <a href="#inherit">glue.inherit</a>.</p>
<hr />
<h2 id="glue.mergedtt1...---dt"><code id="merge">glue.merge(dt,t1,...) -> dt</code></h2>
<p>Update a table with elements of other tables skipping on any existing keys.</p>
<ul>
<li>nil arguments are skipped.</li>
</ul>
<h3 id="examples-3">Examples</h3>
<p>Normalize a data object with default values:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">glue</span><span class="ot">.</span>merge<span class="ot">(</span><span class="kw">t</span><span class="ot">,</span> <span class="kw">defaults</span><span class="ot">)</span></code></pre>
<p>See also: <a href="#update">glue.update</a>.</p>
<hr />
<h2 id="glue.sortedpairstcmp---iteratorkv"><code id="sortedpairs">glue.sortedpairs(t[,cmp]) -> iterator<k,v></code></h2>
<p>Like pairs() but in key order.</p>
<p>The implementation creates a temporary table to sort the keys in.</p>
<p>See also: <a href="#keys">glue.keys</a>.</p>
<hr />
<h2 id="glue.extenddtt1...---dt"><code id="extend">glue.extend(dt,t1,...) -> dt</code></h2>
<p>Extend the list with the elements of other lists.</p>
<ul>
<li>nil arguments are skipped.</li>
<li>list elements are the ones from 1 to <code>#dt</code>.</li>
</ul>
<h3 id="uses">Uses</h3>
<p>Accumulating values from multiple list sources.</p>
<p>See also: <a href="#append">glue.append</a>, <a href="#update">glue.update</a>.</p>
<hr />
<h2 id="glue.appenddtv1...---dt"><code id="append">glue.append(dt,v1,...) -> dt</code></h2>
<p>Append non-nil arguments to a list.</p>
<h3 id="uses-1">Uses</h3>
<p>Appending an object to a flattened list of lists (eg. appending a path element to a 2d path).</p>
<p>See also: <a href="#extend">glue.extend</a>, <a href="#update">glue.update</a>.</p>
<hr />
<h2 id="glue.shifttin---t"><code id="shift">glue.shift(t,i,n) -> t</code></h2>
<p>Shift all the list elements starting at index <code>i</code>, <code>n</code> positions to the left or further to the right.</p>
<p>For a positive <code>n</code>, shift the elements further to the right, effectively creating room for <code>n</code> new elements at index <code>i</code>. When <code>n</code> is 1, the effect is the same as for <code>table.insert(t, i, t[i])</code>. The old values at index <code>i</code> to <code>i+n-1</code> are preserved, so <code>#t</code> still works after the shifting.</p>
<p>For a negative <code>n</code>, shift the elements to the left, effectively removing the <code>n</code> elements at index <code>i</code>. When <code>n</code> is -1, the effect is the same as for <code>table.remove(t, i)</code>.</p>
<h3 id="uses-2">Uses</h3>
<p>Removing a portion of a list or making room for more elements inside the list.</p>
<p>See also: <a href="#extend">glue.extend</a>.</p>
<hr />
<h2 id="glue.gsplitssepplain---iteratorecaptures..."><code id="gsplit">glue.gsplit(s,sep[,plain]) -> iterator<e[,captures...]></code></h2>
<p>Split a string by a separator pattern (or plain string) and iterate over the elements.</p>
<ul>
<li>if sep is "" return the entire string in one iteration</li>
<li>if s is "" return s in one iteration</li>
<li>empty strings between separators are always returned, eg. <code>glue.gsplit(',', ',')</code> produces 2 empty strings</li>
<li>captures are allowed in sep and they are returned after the element, except for the last element for which they don't match (by definition).</li>
</ul>
<h3 id="examples-4">Examples</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">for</span> <span class="kw">s</span> <span class="kw">in</span> <span class="kw">glue</span><span class="ot">.</span>gsplit<span class="ot">(</span><span class="st">'Spam eggs spam spam and ham'</span><span class="ot">,</span> <span class="st">'%s*spam%s*'</span><span class="ot">)</span> <span class="kw">do</span>
<span class="fu">print</span><span class="ot">(</span><span class="st">'"'</span><span class="ot">..</span><span class="kw">s</span><span class="ot">..</span><span class="st">'"'</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="ot">></span> <span class="st">"Spam eggs"</span>
<span class="ot">></span> <span class="st">""</span>
<span class="ot">></span> <span class="st">"and ham"</span></code></pre>
<h3 id="design-notes">Design notes</h3>
<ul>
<li>name choice: associate with <code>gmatch</code> and <code>gsub</code></li>
<li>allowing captures in <code>sep</code> doesn't have very readable semantics</li>
</ul>
<hr />
<h2 id="glue.trims---s"><code id="trim">glue.trim(s) -> s</code></h2>
<p>Remove whitespace (defined as Lua pattern <code>%s</code>) from the beginning and end of a string.</p>
<hr />
<h2 id="glue.escapesmode---pat"><code id="escape">glue.escape(s[,mode]) -> pat</code></h2>
<p>Escape magic characters of the string <code>s</code> so that it can be used as a pattern to string matching functions.</p>
<ul>
<li>the optional argument <code>mode</code> can have the value <code>"*i"</code> (for case insensitive), in which case each alphabetical character in <code>s</code> will also be escaped as <code>[aA]</code> so that it matches both its lowercase and uppercase variants.</li>
<li>escapes embedded zeroes as the <code>%z</code> pattern.</li>
</ul>
<h3 id="uses-3">Uses</h3>
<ul>
<li>workaround for lack of pattern syntax for "this part of a match is an arbitrary string"</li>
<li>workaround for lack of a case-insensitive flag in pattern matching functions</li>
</ul>
<h3 id="design-notes-1">Design notes</h3>
<p>Test the performance of the case-insensitive hack to see if it's feasible.</p>
<hr />
<h2 id="glue.tohexsupper---s"><code id="tohex">glue.tohex(s[,upper]) -> s</code></h2>
<h2 id="glue.tohexnupper---s"><code>glue.tohex(n[,upper]) -> s</code></h2>
<p>Convert a binary string or a Lua number to its hex representation.</p>
<ul>
<li>lowercase by default</li>
<li>uppercase if <code>upper</code> is anything non-false, like say, the string "upper"</li>
<li>numbers must be in the unsigned 32 bit integer range</li>
</ul>
<p>See also: <a href="#fromhex">glue.fromhex</a>.</p>
<hr />
<h2 id="glue.fromhexs---s"><code id="fromhex">glue.fromhex(s) -> s</code></h2>
<p>Convert a hex string to its binary representation.</p>
<p>See also: <a href="#tohex">glue.tohex</a>.</p>
<hr />
<h2 id="glue.collecti-iterator---t"><code id="collect">glue.collect([i, ]iterator) -> t</code></h2>
<p>Iterate an iterator and collect its i'th return value of every step into a list.</p>
<ul>
<li>i defaults to 1</li>
</ul>
<h3 id="examples-5">Examples</h3>
<p>Implementation of <code>keys()</code> and <code>values()</code> in terms of <code>collect()</code></p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">keys</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">t</span><span class="ot">)</span> <span class="kw">return</span> <span class="kw">glue</span><span class="ot">.</span>collect<span class="ot">(</span><span class="fu">pairs</span><span class="ot">(</span><span class="kw">t</span><span class="ot">))</span> <span class="kw">end</span>
<span class="kw">values</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">t</span><span class="ot">)</span> <span class="kw">return</span> <span class="kw">glue</span><span class="ot">.</span>collect<span class="ot">(</span><span class="dv">2</span><span class="ot">,</span><span class="fu">pairs</span><span class="ot">(</span><span class="kw">t</span><span class="ot">))</span> <span class="kw">end</span></code></pre>
<p>Collecting string matches:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">s</span> <span class="ot">=</span> <span class="st">'a,b,c,'</span>
<span class="kw">t</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>collect<span class="ot">(</span><span class="kw">s</span>:gmatch<span class="st">'(.-),'</span><span class="ot">)</span>
<span class="kw">for</span> <span class="kw">i</span><span class="ot">=</span><span class="dv">1</span><span class="ot">,#</span><span class="kw">t</span> <span class="kw">do</span> <span class="fu">print</span><span class="ot">(</span><span class="kw">t</span><span class="ot">[</span><span class="kw">i</span><span class="ot">])</span> <span class="kw">end</span>
<span class="ot">></span> <span class="kw">a</span>
<span class="ot">></span> <span class="kw">b</span>
<span class="ot">></span> <span class="kw">c</span></code></pre>
<h3 id="design-notes-2">Design notes</h3>
<p>Alt. name: <code>ipack</code> - like pack but for iterators; collect is better at suggesting a process done in steps.</p>
<hr />
<h2 id="glue.pass...---..."><code id="pass">glue.pass(...) -> ...</code></h2>
<p>The identity function. Does nothing, returns back all arguments.</p>
<h3 id="uses-4">Uses</h3>
<p>Default value for optional callback arguments:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">function</span> urlopen<span class="ot">(</span><span class="kw">url</span><span class="ot">,</span> <span class="kw">callback</span><span class="ot">,</span> <span class="kw">errback</span><span class="ot">)</span>
<span class="kw">callback</span> <span class="ot">=</span> <span class="kw">callback</span> <span class="kw">or</span> <span class="kw">glue</span><span class="ot">.</span><span class="kw">pass</span>
<span class="kw">errback</span> <span class="ot">=</span> <span class="kw">errback</span> <span class="kw">or</span> <span class="kw">glue</span><span class="ot">.</span><span class="kw">pass</span>
<span class="ot">...</span>
callback<span class="ot">()</span>
<span class="kw">end</span></code></pre>
<hr />
<h2 id="glue.inheritt-parent---t"><code id="inherit">glue.inherit(t, parent) -> t</code></h2>
<h2 id="glue.inheritt-nil---t"><code>glue.inherit(t, nil) -> t</code></h2>
<p>Set a table to inherit attributes from a parent table, or clear inheritance.</p>
<p>If the table has no metatable (and inheritance has to be set, not cleared) make it one.</p>
<h3 id="examples-6">Examples</h3>
<p>Logging mixin:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">AbstractLogger</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>inherit<span class="ot">({},</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">t</span><span class="ot">,</span><span class="kw">k</span><span class="ot">)</span> <span class="fu">error</span><span class="ot">(</span><span class="st">'abstract '</span><span class="ot">..</span><span class="kw">k</span><span class="ot">)</span> <span class="kw">end</span><span class="ot">)</span>
<span class="kw">NullLogger</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>inherit<span class="ot">({</span><span class="fu">log</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">()</span> <span class="kw">end</span><span class="ot">},</span> <span class="kw">AbstractLogger</span><span class="ot">)</span>
<span class="kw">PrintLogger</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>inherit<span class="ot">({</span><span class="fu">log</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">self</span><span class="ot">,...)</span> <span class="fu">print</span><span class="ot">(...)</span> <span class="kw">end</span><span class="ot">},</span> <span class="kw">AbstractLogger</span><span class="ot">)</span>
<span class="kw">HttpRequest</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>inherit<span class="ot">({</span>
<span class="kw">perform</span> <span class="ot">=</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">self</span><span class="ot">,</span> <span class="kw">url</span><span class="ot">)</span>
<span class="kw">self</span>:<span class="fu">log</span><span class="ot">(</span><span class="st">'Requesting'</span><span class="ot">,</span> <span class="kw">url</span><span class="ot">,</span> <span class="st">'...'</span><span class="ot">)</span>
<span class="ot">...</span>
<span class="kw">end</span>
<span class="ot">},</span> <span class="kw">NullLogger</span><span class="ot">)</span>
<span class="kw">LoggedRequest</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>inherit<span class="ot">({</span><span class="fu">log</span> <span class="ot">=</span> <span class="kw">PrintLogger</span><span class="ot">.</span><span class="kw">log</span><span class="ot">},</span> <span class="kw">HttpRequest</span><span class="ot">)</span>
<span class="kw">LoggedRequest</span>:perform<span class="st">'http://lua.org/'</span>
<span class="ot">></span> <span class="kw">Requesting</span> <span class="kw">http</span>:<span class="ot">//</span><span class="kw">lua</span><span class="ot">.</span><span class="kw">org</span><span class="ot">/</span> <span class="ot">...</span></code></pre>
<p>Defining a module in Lua 5.2</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">_ENV</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>inherit<span class="ot">({},</span><span class="kw">_G</span><span class="ot">)</span>
<span class="ot">...</span></code></pre>
<p>Hints:</p>
<ul>
<li>to get the effect of static (single or multiple) inheritance, use <a href="#update">glue.update</a>.</li>
<li>when setting inheritance, you can pass in a function.</li>
</ul>
<h3 id="design-notes-3">Design notes</h3>
<p><code>t = setmetatable({},{__index=parent})</code> is not much longer and it's idiomatic, but doesn't shout inheritance at you (you have to process the indirection, like with functional idioms) and you can't use it to change the parent (a minor quibble nevertheless).</p>
<p>Overriding of methods needs an easy way to access the "parent" or to invoke a method on the parent. A top-level class could provide this simply by defining <code>function Object:parent() return getmetatable(self).__index end</code>.</p>
<hr />
<h2 id="glue.autotablet---t"><code id="autotable">glue.autotable([t]) -> t</code></h2>
<p>Set a table to create/return missing keys as autotables.</p>
<p>In the example below, <code>t.a</code>, <code>t.a.b</code>, <code>t.a.b.c</code> are created automatically as autotables.</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">t</span> <span class="ot">=</span> autotable<span class="ot">()</span>
<span class="kw">t</span><span class="ot">.</span><span class="kw">a</span><span class="ot">.</span><span class="kw">b</span><span class="ot">.</span><span class="kw">c</span><span class="ot">.</span><span class="kw">d</span> <span class="ot">=</span> <span class="st">'hello'</span></code></pre>
<hr />
<h2 id="glue.fileexistsfile---true-false"><code id="fileexists">glue.fileexists(file) -> true | false</code></h2>
<p>Checks whether a file exists and it's available for reading.</p>
<p>See also: <a href="#readfile">glue.readfile</a>.</p>
<hr />
<h2 id="glue.readfilefileformat---s-nil-err"><code id="readfile">glue.readfile(file[,format]) -> s | nil, err</code></h2>
<p>Read the contents of a file into a string.</p>
<ul>
<li><code>format</code> can be <code>t</code> in which case the file will be read in text mode (default is binary mode).</li>
</ul>
<p>See also: <a href="#writefile">glue.writefile</a>, <a href="#fileexists">glue.fileexists</a>.</p>
<hr />
<h2 id="glue.writefilefilesformat"><code id="writefile">glue.writefile(file,s[,format])</code></h2>
<p>Write the contents of a string to a file.</p>
<ul>
<li><code>format</code> can be <code>t</code> in which case the file will be written in text mode (default is binary mode).</li>
</ul>
<p>See also: <a href="#readfile">glue.readfile</a>.</p>
<hr />
<h2 id="glue.assertv-message-format_args..."><code id="assert">glue.assert(v[, message[, format_args...]])</code></h2>
<p>Like <code>assert</code> but supports formatting of the error message using string.format.</p>
<p>This is better than <code>assert(string.format(message, format_args...))</code> because it avoids creating the message string when the assertion is true.</p>
<h3 id="example">Example</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">glue</span><span class="ot">.</span>assert<span class="ot">(</span><span class="kw">depth</span> <span class="ot"><=</span> <span class="kw">maxdepth</span><span class="ot">,</span> <span class="st">'maximum depth %d exceeded'</span><span class="ot">,</span> <span class="kw">maxdepth</span><span class="ot">)</span></code></pre>
<hr />
<h2 id="glue.unprotectokresult...---result...-nilresult..."><code id="unprotect">glue.unprotect(ok,result,...) -> result,... | nil,result,...</code></h2>
<p>In Lua, API functions conventionally signal errors by returning nil and an error message instead of raising exceptions. In the implementation however, using assert() and error() is preferred to coding explicit conditional flows to cover exceptional cases. Use this function to convert error-raising functions to nice nil,error-returning functions:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">function</span> my_API_function<span class="ot">()</span>
<span class="kw">return</span> <span class="kw">glue</span><span class="ot">.</span>unprotect<span class="ot">(</span><span class="fu">pcall</span><span class="ot">(</span><span class="kw">function</span><span class="ot">()</span>
<span class="ot">...</span>
<span class="fu">assert</span><span class="ot">(...)</span>
<span class="ot">...</span>
<span class="fu">error</span><span class="ot">(...)</span>
<span class="ot">...</span>
<span class="kw">return</span> <span class="kw">result_value</span>
<span class="kw">end</span><span class="ot">))</span>
<span class="kw">end</span></code></pre>
<hr />
<h2 id="glue.pcallf...---true...-falseerror..n..traceback"><code id="pcall">glue.pcall(f,...) -> true,... | false,error..'\n'..traceback</code></h2>
<p>With Lua's pcall() you lose the stack trace, and with usual uses of pcall() you don't want that. This variant appends the traceback to the error message.</p>
<blockquote>
<p><strong>NOTE</strong>: Lua 5.2 and LuaJIT only.</p>
</blockquote>
<hr />
<h2 id="glue.fpcallf...---result-nilerror..n..traceback"><code id="fpcall">glue.fpcall(f,...) -> result | nil,error..'\n'..traceback</code></h2>
<h2 id="glue.fcallf...---result"><code>glue.fcall(f,...) -> result</code></h2>
<p>These constructs bring the ubiquitous try/finally/except idiom to Lua. The first variant returns nil,error when errors occur while the second re-raises the error.</p>
<h3 id="example-1">Example</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">result</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>fpcall<span class="ot">(</span><span class="kw">function</span><span class="ot">(</span><span class="kw">finally</span><span class="ot">,</span> <span class="kw">except</span><span class="ot">,</span> <span class="ot">...)</span>
<span class="kw">local</span> <span class="kw">temporary_resource</span> <span class="ot">=</span> acquire_resource<span class="ot">()</span>
finally<span class="ot">(</span><span class="kw">function</span><span class="ot">()</span> <span class="kw">temporary_resource</span>:free<span class="ot">()</span> <span class="kw">end</span><span class="ot">)</span>
<span class="ot">...</span>
<span class="kw">local</span> <span class="kw">final_resource</span> <span class="ot">=</span> acquire_resource<span class="ot">()</span>
except<span class="ot">(</span><span class="kw">function</span><span class="ot">()</span> <span class="kw">final_resource</span>:free<span class="ot">()</span> <span class="kw">end</span><span class="ot">)</span>
<span class="ot">...</span> <span class="kw">code</span> <span class="kw">that</span> <span class="kw">might</span> <span class="kw">break</span> <span class="ot">...</span>
<span class="kw">return</span> <span class="kw">final_resource</span>
<span class="kw">end</span><span class="ot">,</span> <span class="ot">...)</span></code></pre>
<hr />
<h2 id="glue.autoloadt-submodules---t"><code id="autoload">glue.autoload(t, submodules) -> t</code></h2>
<h2 id="glue.autoloadt-key-moduleloader---t"><code>glue.autoload(t, key, module|loader) -> t</code></h2>
<p>Assign a metatable to <code>t</code> such that when a missing key is accessed, the module said to contain that key is require'd automatically.</p>
<p>The <code>submodules</code> argument is a table of form <code>{key = module_name | load_function}</code> specifying the corresponding Lua module (or load function) that make each key available to <code>t</code>. The alternative syntax allows specifying the key - submodule associations one by one.</p>
<h3 id="motivation">Motivation</h3>
<p>Module autoloading allows you to split the implementation of a module in many submodules containing optional, self-contained functionality, without having to make this visible in the user API. This effectively separates how you split your APIs from how you split the implementation, allowing you to change the way the implementation is split at a later time while keeping the API intact.</p>
<h3 id="example-2">Example</h3>
<p><strong>main module (foo.lua):</strong></p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">function</span> bar<span class="ot">()</span> <span class="co">--function implemented in the main module</span>
<span class="ot">...</span>
<span class="kw">end</span>
<span class="co">--create and return the module table</span>
<span class="kw">return</span> <span class="kw">glue</span><span class="ot">.</span>autoload<span class="ot">({</span>
<span class="ot">...</span>
<span class="kw">bar</span> <span class="ot">=</span> <span class="kw">bar</span><span class="ot">,</span>
<span class="ot">},</span> <span class="ot">{</span>
<span class="kw">baz</span> <span class="ot">=</span> <span class="st">'foo_extra'</span><span class="ot">,</span> <span class="co">--autoloaded function, implemented in module foo_extra</span>
<span class="ot">})</span></code></pre>
<p><strong>submodule (foo_extra.lua):</strong></p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">foo</span> <span class="ot">=</span> require<span class="st">'foo'</span>
<span class="kw">function</span> <span class="kw">foo</span><span class="ot">.</span>baz<span class="ot">(...)</span>
<span class="ot">...</span>
<span class="kw">end</span></code></pre>
<p><strong>in usage:</strong></p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">foo</span> <span class="ot">=</span> require<span class="st">'foo'</span>
<span class="kw">foo</span><span class="ot">.</span>baz<span class="ot">(...)</span> <span class="co">-- foo_extra was now loaded automatically</span></code></pre>
<hr />
<h2 id="glue.bin"><code id="bin">glue.bin</code></h2>
<p>Get the script's directory, based on <a href="https://github.com/davidm/lua-find-bin">lua-find-bin</a> by David Manura. This allows finding files in the script's directory regardless of the directory that Lua is started in.</p>
<h3 id="example-3">Example</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">foobar</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>readfile<span class="ot">(</span><span class="kw">glue</span><span class="ot">.</span><span class="kw">bin</span> <span class="ot">..</span> <span class="st">'/'</span> <span class="ot">..</span> <span class="kw">file_near_this_script</span><span class="ot">)</span></code></pre>
<h3 id="caveats">Caveats</h3>
<p>This only works if glue itself can already be found and required (chicken/egg catch22 and the rest). The path is relative to the current directory, it stops working if the current directory is changed. The assumption is that if you can do chdir() then you can also do getfullpath() or at least getcurrentdir(), and thus correct <code>glue.bin</code> in time with:</p>
<pre><code>glue.bin = getfullpath(glue.bin)</code></pre>
<p>or</p>
<pre><code>glue.bin = getcurrentdir() .. '/' .. glue.bin</code></pre>
<hr />
<h2 id="glue.luapathpath-index-ext"><code id="luapath">glue.luapath(path[, index[, ext]])</code></h2>
<p>Insert a Lua search pattern in <code>package.path</code> such that <code>require</code> will be able to load Lua modules from that path. The optional <code>index</code> arg specifies the insert position (default is 1, that is, before all existing paths; can be negative, to start counting from the end; can be the string 'after', which is the same as 0). The optional <code>ext</code> arg specifies the file extension to use (default is "lua").</p>
<h2 id="glue.cpathpath-index"><code id="cpath">glue.cpath(path[, index])</code></h2>
<p>Insert a Lua search pattern in <code>package.cpath</code> such that <code>require</code> will be able to load Lua/C modules from that path. The <code>index</code> arg has the same meaning as with <code>glue.luapath</code>.</p>
<h3 id="example-4">Example</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">glue</span><span class="ot">.</span>luapath<span class="ot">(</span><span class="kw">glue</span><span class="ot">.</span><span class="kw">bin</span><span class="ot">)</span>
<span class="kw">glue</span><span class="ot">.</span>cpath<span class="ot">(</span><span class="kw">glue</span><span class="ot">.</span><span class="kw">bin</span><span class="ot">)</span>
require<span class="st">'foo'</span> <span class="co">--looking for `foo` in the same directory as the running script first</span></code></pre>
<hr />
<h2 id="glue.mallocctype-size---cdata"><code id="malloc-array">glue.malloc([ctype, ]size) -> cdata</code></h2>
<p>Allocate a <code>ctype[size]</code> array with system's malloc. Useful for allocating larger chunks of memory without hitting the default allocator's 2 GB limit.</p>
<ul>
<li>the returned cdata has the type <code>ctype(&)[size]</code> so ffi.sizeof(cdata) returns the correct size.</li>
<li><code>ctype</code> defaults to <code>char</code>.</li>
<li>failure to allocate results in error.</li>
<li>the memory is freed when the cdata gets collected or with <code>glue.free()</code>.</li>
</ul>
<p><strong>REMEMBER!</strong> Just like with <code>ffi.new</code>, casting the result cdata further will get you <em>weak references</em> to the allocated memory. To transfer ownership of the memory, use <code>ffi.gc(original, nil); ffi.gc(pointer, glue.free)</code>.</p>
<blockquote>
<p><strong>NOTE</strong>: LuaJIT only.</p>
</blockquote>
<blockquote>
<p><strong>CAVEAT</strong>: For primitive types, you must specify a size, or glue.free() will not work!</p>
</blockquote>
<h2 id="glue.mallocctype---cdata"><code id="malloc-ctype">glue.malloc(ctype) -> cdata</code></h2>
<p>Allocate a <code>ctype</code> with system's malloc. The result has the type <code>ctype&</code>.</p>
<h2 id="glue.freecdata"><code id="free">glue.free(cdata)</code></h2>
<p>Free malloc'ed memory.</p>
<h3 id="example-5">Example</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">data</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>malloc<span class="ot">(</span><span class="dv">100</span><span class="ot">)</span>
<span class="fu">assert</span><span class="ot">(</span><span class="kw">ffi</span><span class="ot">.</span>sizeof<span class="ot">(</span><span class="kw">data</span><span class="ot">)</span> <span class="ot">==</span> <span class="dv">100</span><span class="ot">)</span>
<span class="kw">glue</span><span class="ot">.</span>free<span class="ot">(</span><span class="kw">data</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">data</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>malloc<span class="ot">(</span><span class="st">'int'</span><span class="ot">,</span> <span class="dv">100</span><span class="ot">)</span>
<span class="fu">assert</span><span class="ot">(</span><span class="kw">ffi</span><span class="ot">.</span>sizeof<span class="ot">(</span><span class="kw">data</span><span class="ot">)</span> <span class="ot">==</span> <span class="dv">100</span> <span class="ot">*</span> <span class="kw">ffi</span><span class="ot">.</span>sizeof<span class="st">'int'</span><span class="ot">)</span>
<span class="kw">glue</span><span class="ot">.</span>free<span class="ot">(</span><span class="kw">data</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">data</span> <span class="ot">=</span> <span class="kw">glue</span><span class="ot">.</span>malloc<span class="ot">(</span><span class="st">'struct S'</span><span class="ot">)</span>
<span class="fu">assert</span><span class="ot">(</span><span class="kw">ffi</span><span class="ot">.</span>typeof<span class="ot">(</span><span class="kw">data</span><span class="ot">)</span> <span class="ot">==</span>
<span class="fu">assert</span><span class="ot">(</span><span class="kw">ffi</span><span class="ot">.</span>sizeof<span class="ot">(</span><span class="kw">data</span><span class="ot">)</span> <span class="ot">==</span> <span class="kw">ffi</span><span class="ot">.</span>sizeof<span class="st">'struct S'</span><span class="ot">)</span>
<span class="kw">glue</span><span class="ot">.</span>free<span class="ot">(</span><span class="kw">data</span><span class="ot">)</span></code></pre>
<hr />
<h2 id="tips">Tips</h2>
<p>String functions are also in the <code>glue.string</code> table. You can extend the Lua <code>string</code> namespace:</p>
<pre><code>glue.update(string, glue.string)</code></pre>
<p>so you can use them as string methods:</p>
<pre><code>s = s:trim()</code></pre>
<h2 id="keywords">Keywords</h2>
<p><em>for syntax highlighting</em></p>
<p>glue.index, glue.keys, glue.update, glue.merge, glue.extend, glue.append, glue.shift, glue.gsplit, glue.trim, glue.escape, glue.collect, glue.pass, glue.inherit, glue.fileexists, glue.readfile, glue.writefile, glue.assert, glue.unprotect, glue.pcall, glue.fpcall, glue.fcall, glue.autoload, glue.bin, glue.luapath, glue.cpath</p>
<h2 id="design">Design</h2>
<p><a href="glue_design.html">glue_design</a></p>
</section>
</div>
<div id="tab2_container" class="doc"></div>
</div>
</div>
<div class="container">
<footer>
<div id="disqus_thread"></div>
<div class="faint">[email protected] | <a href="http://unlicense.org/">public domain</a></div>
</footer>
</div>
</div>
<script type="text/x-mustache" id=info_tab_template>
<h3>Modules</h3>
<ul>
{{#module_array}}
<li>{{name}}</li>
{{/module_array}}
</ul>
</script>
</body>
</html>