/
robotskirt.cc
1111 lines (980 loc) · 47.1 KB
/
robotskirt.cc
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "v8u.hpp"
#include "version.hpp"
#include <node_buffer.h>
#include <string>
#include <cstring>
extern "C" {
#include "markdown.h"
#include "html.h"
#include "houdini.h"
}
using namespace std;
using namespace node;
using namespace v8;
using namespace v8u;
//using namespace v8u::Version
namespace robotskirt {
// Constants taken from the official Sundown executable
#define OUTPUT_UNIT 64
#define DEFAULT_MAX_NESTING 16
////////////////////////////////////////////////////////////////////////////////
// UTILITIES to ease wrapping and interfacing with V8
////////////////////////////////////////////////////////////////////////////////
// Credit: @samcday
// http://sambro.is-super-awesome.com/2011/03/03/creating-a-proper-buffer-in-a-node-c-addon/
#define MAKE_FAST_BUFFER(NG_SLOW_BUFFER, NG_FAST_BUFFER) \
Local<Function> NG_JS_BUFFER = Local<Function>::Cast( \
Context::GetCurrent()->Global()->Get( \
String::New("Buffer"))); \
\
Handle<Value> NG_JS_ARGS[2] = { \
NG_SLOW_BUFFER, \
Integer::New(Buffer::Length(NG_SLOW_BUFFER))/*, \
Integer::New(0) <- WITH THIS WILL THROW AN ERROR*/ \
}; \
\
NG_FAST_BUFFER = NG_JS_BUFFER->NewInstance(2, NG_JS_ARGS);
//DEPRECATED: Use Int() or Uint()
inline int64_t CheckInt(Handle<Value> value) {
if (!value->IsInt32()) V8_THROW(TypeErr("You must provide an integer!"));
return value->IntegerValue();
}
int CheckFlags(Handle<Value> hdl) {
HandleScope scope;
if (hdl->IsArray()) {
int ret = 0;
Handle<Array> array = Handle<Array>::Cast(hdl);
for (uint32_t i=0; i<array->Length(); i++)
ret |= Int(array->Get(i));
return ret;
}
return Int(hdl);
}
unsigned int CheckUFlags(Handle<Value> hdl) {
HandleScope scope;
if (hdl->IsArray()) {
unsigned int ret = 0;
Handle<Array> array = Handle<Array>::Cast(hdl);
for (uint32_t i=0; i<array->Length(); i++)
ret |= Uint(array->Get(i));
return ret;
}
return Uint(hdl);
}
//A reference counter
class RendFuncData {
public:
RendFuncData() : refs(0), unrefs(0) {}
void unref() {
unrefs++;
if (unrefs > refs) delete this;
}
void ref() {
refs++;
}
virtual void* ptr() = 0;
virtual ~RendFuncData() {};
private:
unsigned char refs;
unsigned char unrefs;
};
class HtmlRendFuncData : public RendFuncData {
public:
HtmlRendFuncData() : opt(new html_renderopt) {}
~HtmlRendFuncData() {delete opt;}
void* ptr() {return opt;};
private:
html_renderopt* const opt;
};
////////////////////////////////////////////////////////////////////////////////
// RENDER FUNCTIONS STUFF
////////////////////////////////////////////////////////////////////////////////
// SIGNATURES
enum CppSignature {
void_BUF1,
void_BUF2,
void_BUF2INT,
void_BUF3,
int_BUF1,
int_BUF2,
int_BUF2INT,
int_BUF4
};
// A C++ WRAPPER FOR Buf*, to ensure deallocation
class BufWrap {
public:
explicit BufWrap(buf* buf): buf_(buf) {}
~BufWrap() {
bufrelease(buf_);
}
buf* get() {return buf_;}
buf* operator->() {return buf_;}
buf* operator*() {return buf_;}
private:
buf* const buf_;
};
// CONVERTERS (especially buf* to Local<Object>)
//DEPRECATED: use toString instead
Local<Object> toBuffer(const buf* buf) {
HandleScope scope;
Local<Object> ret;
Handle<Object> buffer = Buffer::New(String::New((char*)buf->data, buf->size));
MAKE_FAST_BUFFER(buffer, ret);
return scope.Close(ret);
}
//DEPRECATED: unsafe, use makeBuf instead
void setToBuf(buf* target, Handle<Object> obj) {
bufreset(target);
target->data = (uint8_t*)Buffer::Data(obj);
target->asize = target->size = Buffer::Length(obj);
Buffer::Initialize(obj);
}
inline void putToBuf(buf* target, Handle<Value> obj) {
String::Utf8Value str (obj);
bufput(target, *str, str.length());
}
inline Handle<Value> toString(const buf* buf) {
if (!buf) return Null();
return String::New(reinterpret_cast<const char*>(buf->data), buf->size);
}
inline void makeBuf(buf*& target, String::Utf8Value*& txt, Local<Value> value) {
if (value->IsUndefined() || value->IsNull()) return;
txt = new String::Utf8Value(value);
target = new buf;
target->data = (uint8_t*)(**txt);
target->size = target->asize = txt->length();
target->unit = 0;
}
// FUNCTION DATA (this gets injected into CPP functions converted to JS)
class FunctionData;
typedef v8::Handle<v8::Value> (*PassInvocationCallback)(robotskirt::FunctionData*, const v8::Arguments&);
class FunctionData: public ObjectWrap {
public:
V8_CL_WRAPPER("robotskirt::FunctionData")
static Handle<Value> NewInstance(const v8::Arguments& args) {
v8::HandleScope scope;
if ((args.Length()==1) && (args[0]->IsExternal())) {
((FunctionData*)External::Unwrap(args[0]))->Wrap(args.This());
return scope.Close(args.This());
}
return ThrowException(Err("You can't instantaniate this from the JS side."));
}
FunctionData(void* function, CppSignature signature, RendFuncData* opaque, PassInvocationCallback wrapper):
function_(function), signature_(signature), opaque_(opaque), wrapper_(wrapper) {
opaque->ref();
}
~FunctionData() {
opaque_->unref();
}
void* getFunction() {return function_;}
void* getOpaque() {return opaque_->ptr();}
CppSignature getSignature() {return signature_;}
static V8_CALLBACK(ToString) {
return scope.Close(Str("<Native function>"));
} V8_CALLBACK_END()
static Handle<Value> Call(const Arguments& args) {
HandleScope scope;
V8_UNWRAP(FunctionData, args)
return inst->wrapper_(inst, args);
}
NODE_DEF_TYPE("NativeFunction") {
V8_DEF_METHOD(ToString, "toString");
V8_DEF_METHOD(ToString, "inspect");
prot->InstanceTemplate()->SetCallAsFunctionHandler(Call);
StoreTemplate("robotskirt::FunctionData", prot);
} NODE_DEF_TYPE_END()
protected:
void* const function_;
CppSignature const signature_;
RendFuncData* const opaque_;
PassInvocationCallback wrapper_;
};
// CONVERT BETWEEN CPP AND JS FUNCTIONS
bool setCppFunction(void** func, void** opaque, Handle<Object> obj, CppSignature sig) {
HandleScope scope;
if (!GetTemplate("robotskirt::FunctionData")->HasInstance(obj)) return false;
FunctionData* data = ObjectWrap::Unwrap<FunctionData>(obj);
if (sig != data->getSignature()) return false;
*func = data->getFunction();
*opaque = data->getOpaque();
return true;
}
Local<Object> jsFunction(void* func, CppSignature sig, PassInvocationCallback wrapper, RendFuncData* opaque) {
return (new FunctionData(func,sig,opaque,wrapper))->Wrapped();
}
// WRAPPERS (call a [wrapped] CPP function from JS)
#define W_OUTPUT_UNIT OUTPUT_UNIT
#define WRAPPER_CALL_void()
#define WRAPPER_CALL_int() int v =
#define WRAPPER_POST_CALL_void()
#define WRAPPER_POST_CALL_int() \
if (!v) return False();
#define WRAPPERS(SIGBASE) SIGBASE##_WRAPPER(void) SIGBASE##_WRAPPER(int)
// For the wrappers we don't use V8U wrapping,
// as we don't need any of his features.
#define BUF1_WRAPPER(RET) \
Handle<Value> BUF1_wrapper_##RET(FunctionData* inst, const Arguments& args) {\
BufWrap ob (bufnew(W_OUTPUT_UNIT)); \
WRAPPER_CALL_##RET() ((RET(*)(buf*, void*))inst->getFunction()) \
(*ob, inst->getOpaque()); \
WRAPPER_POST_CALL_##RET() \
return toString(*ob); \
}
WRAPPERS(BUF1)
#define BUF2_WRAPPER(RET) \
Handle<Value> BUF2_wrapper_##RET(FunctionData* inst, const Arguments& args) {\
if(args.Length()<1) return ThrowException(RangeErr("Not enough arguments."));\
String::Utf8Value* texts = NULL; \
buf* text = NULL; \
makeBuf(text, texts, args[0]); \
\
BufWrap ob (bufnew(W_OUTPUT_UNIT)); \
WRAPPER_CALL_##RET() ((RET(*)(buf*, const buf*, void*))inst->getFunction())\
(*ob, text, inst->getOpaque()); \
if (texts != NULL) { delete texts; delete text; } \
WRAPPER_POST_CALL_##RET() \
return toString(*ob); \
}
WRAPPERS(BUF2)
#define BUF2INT_WRAPPER(RET) \
Handle<Value> BUF2INT_wrapper_##RET(FunctionData* inst, const Arguments& args) {\
if(args.Length()<2) return ThrowException(RangeErr("Not enough arguments."));\
String::Utf8Value* texts = NULL; \
buf* text = NULL; \
makeBuf(text, texts, args[0]); \
\
BufWrap ob (bufnew(W_OUTPUT_UNIT)); \
WRAPPER_CALL_##RET() ((RET(*)(buf*, const buf*, int, void*))inst->getFunction())\
(*ob, text, Int(args[1]), inst->getOpaque()); \
if (texts != NULL) { delete texts; delete text; } \
WRAPPER_POST_CALL_##RET() \
return toString(*ob); \
}
WRAPPERS(BUF2INT)
#define BUF3_WRAPPER(RET) \
Handle<Value> BUF3_wrapper_##RET(FunctionData* inst, const Arguments& args) {\
if(args.Length()<2) return ThrowException(RangeErr("Not enough arguments."));\
String::Utf8Value* texts = NULL; \
buf* text = NULL; \
makeBuf(text, texts, args[0]); \
\
String::Utf8Value* langs = NULL; \
buf* lang = NULL; \
makeBuf(lang, langs, args[1]); \
\
BufWrap ob (bufnew(W_OUTPUT_UNIT)); \
WRAPPER_CALL_##RET() ((RET(*)(buf*, const buf*, const buf*, void*))inst->getFunction())\
(*ob, text, lang, inst->getOpaque()); \
if (texts != NULL) { delete texts; delete text; } \
if (langs != NULL) { delete langs; delete lang; } \
WRAPPER_POST_CALL_##RET() \
return toString(*ob); \
}
WRAPPERS(BUF3)
#define BUF4_WRAPPER(RET) \
Handle<Value> BUF4_wrapper_##RET(FunctionData* inst, const Arguments& args) {\
if(args.Length()<3) return ThrowException(RangeErr("Not enough arguments."));\
String::Utf8Value* links = NULL; \
buf* link = NULL; \
makeBuf(link, links, args[0]); \
\
String::Utf8Value* titles = NULL; \
buf* title = NULL; \
makeBuf(title, titles, args[1]); \
\
String::Utf8Value* conts = NULL; \
buf* cont = NULL; \
makeBuf(cont, conts, args[2]); \
\
BufWrap ob (bufnew(W_OUTPUT_UNIT)); \
WRAPPER_CALL_##RET() ((RET(*)(buf*, const buf*, const buf*, const buf*, void*))inst->getFunction())\
(*ob, link, title, cont, inst->getOpaque()); \
if (links != NULL) { delete links; delete link; } \
if (titles != NULL) { delete titles; delete title; } \
if (conts != NULL) { delete conts; delete cont; } \
WRAPPER_POST_CALL_##RET() \
return toString(*ob); \
}
WRAPPERS(BUF4)
// BINDERS (call a JS function from CPP)
#define BINDER_RETURN_void
#define BINDER_RETURN_int 1
#define BINDER_RETURN_NULL_void
#define BINDER_RETURN_NULL_int 0
#define BUF1_BINDER(CPPFUNC, RET) \
static RET CPPFUNC##_binder(struct buf *ob, void *opaque) { \
HandleScope scope; \
\
/*Convert arguments*/ \
Handle<Value> args [0]; \
\
/*Call it!*/ \
TryCatch trycatch; \
Local<Value> ret = ((RendererData*)opaque)->CPPFUNC->CallAsFunction(Context::GetCurrent()->Global(), 0, args);\
if (trycatch.HasCaught()) \
V8_THROW(trycatch.Exception()); \
/*Convert the result back*/ \
if (ret->IsFalse()) return BINDER_RETURN_NULL_##RET; \
putToBuf(ob, ret); \
return BINDER_RETURN_##RET; \
}
#define BUF2_BINDER(CPPFUNC, RET) \
static RET CPPFUNC##_binder(struct buf *ob, const struct buf *text, void *opaque) {\
HandleScope scope; \
\
/*Convert arguments*/ \
Handle<Value> args [1] = {toString(text)}; \
\
/*Call it!*/ \
TryCatch trycatch; \
Local<Value> ret = ((RendererData*)opaque)->CPPFUNC->CallAsFunction(Context::GetCurrent()->Global(), 1, args);\
if (trycatch.HasCaught()) \
V8_THROW(trycatch.Exception()); \
/*Convert the result back*/ \
if (ret->IsFalse()) return BINDER_RETURN_NULL_##RET; \
putToBuf(ob, ret); \
return BINDER_RETURN_##RET; \
}
#define BUF2INT_BINDER(CPPFUNC, RET) \
static RET CPPFUNC##_binder(struct buf *ob, const struct buf *text, int flags, void *opaque) {\
HandleScope scope; \
\
/*Convert arguments*/ \
Handle<Value> args [2] = {toString(text), Int(flags)}; \
\
/*Call it!*/ \
TryCatch trycatch; \
Local<Value> ret = ((RendererData*)opaque)->CPPFUNC->CallAsFunction(Context::GetCurrent()->Global(), 2, args);\
if (trycatch.HasCaught()) \
V8_THROW(trycatch.Exception()); \
/*Convert the result back*/ \
if (ret->IsFalse()) return BINDER_RETURN_NULL_##RET; \
putToBuf(ob, ret); \
return BINDER_RETURN_##RET; \
}
#define BUF3_BINDER(CPPFUNC, RET) \
static RET CPPFUNC##_binder(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque) {\
HandleScope scope; \
\
/*Convert arguments*/ \
Handle<Value> args [2] = {toString(text), toString(lang)}; \
\
/*Call it!*/ \
TryCatch trycatch; \
Local<Value> ret = ((RendererData*)opaque)->CPPFUNC->CallAsFunction(Context::GetCurrent()->Global(), 2, args);\
if (trycatch.HasCaught()) \
V8_THROW(trycatch.Exception()); \
/*Convert the result back*/ \
if (ret->IsFalse()) return BINDER_RETURN_NULL_##RET; \
putToBuf(ob, ret); \
return BINDER_RETURN_##RET; \
}
#define BUF4_BINDER(CPPFUNC, RET) \
static RET CPPFUNC##_binder(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *cont, void *opaque) {\
HandleScope scope; \
\
/*Convert arguments*/ \
Handle<Value> args [3] = {toString(link), toString(title), toString(cont)};\
\
/*Call it!*/ \
TryCatch trycatch; \
Local<Value> ret = ((RendererData*)opaque)->CPPFUNC->CallAsFunction(Context::GetCurrent()->Global(), 3, args);\
if (trycatch.HasCaught()) \
V8_THROW(trycatch.Exception()); \
/*Convert the result back*/ \
if (ret->IsFalse()) return BINDER_RETURN_NULL_##RET; \
putToBuf(ob, ret); \
return BINDER_RETURN_##RET; \
}
// FORWARDERS (forward a Sundown call to its original C++ renderer)
#define BUF1_FORWARDER(CPPFUNC, RET) \
static RET CPPFUNC##_forwarder(struct buf *ob, void *opaque) { \
RendererData* rend = (RendererData*)opaque; \
return ((RET(*)(struct buf *ob, void *opaque))rend->CPPFUNC##_orig)( \
ob, \
rend->CPPFUNC##_opaque); \
}
#define BUF2_FORWARDER(CPPFUNC, RET) \
static RET CPPFUNC##_forwarder(struct buf *ob, const struct buf *text, void *opaque) {\
RendererData* rend = (RendererData*)opaque; \
return ((RET(*)(struct buf *ob, const struct buf *text, void *opaque))rend->CPPFUNC##_orig)(\
ob, text, \
rend->CPPFUNC##_opaque); \
}
#define BUF2INT_FORWARDER(CPPFUNC, RET) \
static RET CPPFUNC##_forwarder(struct buf *ob, const struct buf *text, int flags, void *opaque) {\
RendererData* rend = (RendererData*)opaque; \
return ((RET(*)(struct buf *ob, const struct buf *text, int flags, void *opaque))rend->CPPFUNC##_orig)(\
ob, text, flags, \
rend->CPPFUNC##_opaque); \
}
#define BUF3_FORWARDER(CPPFUNC, RET) \
static RET CPPFUNC##_forwarder(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque) {\
RendererData* rend = (RendererData*)opaque; \
return ((RET(*)(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque))rend->CPPFUNC##_orig)(\
ob, text, lang, \
rend->CPPFUNC##_opaque); \
}
#define BUF4_FORWARDER(CPPFUNC, RET) \
static RET CPPFUNC##_forwarder(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *cont, void *opaque) {\
RendererData* rend = (RendererData*)opaque; \
return ((RET(*)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *cont, void *opaque))rend->CPPFUNC##_orig)(\
ob, link, title, cont, \
rend->CPPFUNC##_opaque); \
}
//TODO
////////////////////////////////////////////////////////////////////////////////
// RENDERER CLASS DECLARATION
////////////////////////////////////////////////////////////////////////////////
// JS ACCESSORS
#define _RENDFUNC_GETTER(CPPFUNC) \
static V8_GETTER(CPPFUNC##_getter) { \
V8_UNWRAP(RendererWrap, info) \
if (inst->CPPFUNC.IsEmpty()) return scope.Close(Undefined()); \
return scope.Close(*(inst->CPPFUNC)); \
} V8_GETTER_END()
#define _RENDFUNC_SETTER(CPPFUNC, SIGNATURE) \
static V8_SETTER(CPPFUNC##_setter) { \
V8_UNWRAP(RendererWrap, info) \
if (!Bool(value)) { \
inst->CPPFUNC.Clear(); \
return; \
} \
if (!value->IsObject()) V8_THROW(TypeErr("Value must be a function!"));\
Local<Object> obj = Obj(value); \
if (!obj->IsCallable()) V8_THROW(TypeErr("Value must be a function!"));\
\
inst->CPPFUNC = obj; \
} V8_SETTER_END()
// The final macros (defining)
#define RENDFUNC_DEF(CPPFUNC, SIGBASE, RET) \
protected: Persisted<Object> CPPFUNC; \
public: \
_RENDFUNC_GETTER(CPPFUNC) \
_RENDFUNC_SETTER(CPPFUNC, RET##_##SIGBASE) \
\
SIGBASE##_FORWARDER(CPPFUNC,RET) SIGBASE##_BINDER(CPPFUNC,RET)
#define RENDFUNC_DATA(CPPFUNC) \
void* CPPFUNC##_opaque; \
void* CPPFUNC##_orig; \
Persisted<Object> CPPFUNC;
// The final macros (wrapping / making)
#define RENDFUNC_WRAP(CPPFUNC, SIGBASE, RET) \
if (cb->CPPFUNC) \
CPPFUNC = jsFunction((void*)cb->CPPFUNC, RET##_##SIGBASE, &SIGBASE##_wrapper_##RET, opaque);
#define RENDFUNC_MAKE(CPPFUNC, SIGBASE, RET) \
if (!CPPFUNC.IsEmpty()) { \
opaque->CPPFUNC = CPPFUNC; \
if (setCppFunction((void**)&(opaque->CPPFUNC##_orig), (void**)&(opaque->CPPFUNC##_opaque), *CPPFUNC, RET##_##SIGBASE))\
cb->CPPFUNC = &CPPFUNC##_forwarder; \
else cb->CPPFUNC = &CPPFUNC##_binder; \
}
//Special case for autolink
#define RENDFUNC_MAKE_AL(CPPFUNC, SIGBASE, RET) \
if (!CPPFUNC.IsEmpty()) { \
opaque->CPPFUNC = CPPFUNC; \
if (setCppFunction((void**)&(opaque->CPPFUNC##_orig), (void**)&(opaque->CPPFUNC##_opaque), *CPPFUNC, RET##_##SIGBASE))\
cb->CPPFUNC = (int(*)(buf*,const buf*,mkd_autolink,void*))&CPPFUNC##_forwarder;\
else cb->CPPFUNC = (int(*)(buf*,const buf*,mkd_autolink,void*))&CPPFUNC##_binder;\
}
//Define V8 accessors
#define RENDFUNC_V8_DEF(NAME, CPPFUNC) \
prot->InstanceTemplate()->SetAccessor(String::NewSymbol(NAME), \
RendererWrap::CPPFUNC##_getter, RendererWrap::CPPFUNC##_setter);
// Forward declaration, to make things work
class Markdown;
class RendererData {
public:
RENDFUNC_DATA(blockcode)
RENDFUNC_DATA(blockquote)
RENDFUNC_DATA(blockhtml)
RENDFUNC_DATA(header)
RENDFUNC_DATA(hrule)
RENDFUNC_DATA(list)
RENDFUNC_DATA(listitem)
RENDFUNC_DATA(paragraph)
RENDFUNC_DATA(table)
RENDFUNC_DATA(table_row)
RENDFUNC_DATA(table_cell)
RENDFUNC_DATA(autolink)
RENDFUNC_DATA(codespan)
RENDFUNC_DATA(double_emphasis)
RENDFUNC_DATA(emphasis)
RENDFUNC_DATA(image)
RENDFUNC_DATA(linebreak)
RENDFUNC_DATA(link)
RENDFUNC_DATA(raw_html_tag)
RENDFUNC_DATA(triple_emphasis)
RENDFUNC_DATA(strikethrough)
RENDFUNC_DATA(superscript)
RENDFUNC_DATA(entity)
RENDFUNC_DATA(normal_text)
RENDFUNC_DATA(doc_header)
RENDFUNC_DATA(doc_footer)
};
class RendererWrap: public ObjectWrap {
public:
V8_CL_WRAPPER("robotskirt::RendererWrap")
RendererWrap() {}
virtual ~RendererWrap() {}
V8_CL_CTOR(RendererWrap) {
inst = new RendererWrap();
} V8_CL_CTOR_END()
void makeRenderer(sd_callbacks* cb, RendererData* opaque) {
memset(cb, 0, sizeof(*cb));
RENDFUNC_MAKE(blockcode, BUF3, void)
RENDFUNC_MAKE(blockquote, BUF2, void)
RENDFUNC_MAKE(blockhtml, BUF2, void)
RENDFUNC_MAKE(header, BUF2INT, void)
RENDFUNC_MAKE(hrule, BUF1, void)
RENDFUNC_MAKE(list, BUF2INT, void)
RENDFUNC_MAKE(listitem, BUF2INT, void)
RENDFUNC_MAKE(paragraph, BUF2, void)
RENDFUNC_MAKE(table, BUF3, void)
RENDFUNC_MAKE(table_row, BUF2, void)
RENDFUNC_MAKE(table_cell, BUF2INT, void)
RENDFUNC_MAKE_AL(autolink, BUF2INT, int)
RENDFUNC_MAKE(codespan, BUF2, int)
RENDFUNC_MAKE(double_emphasis, BUF2, int)
RENDFUNC_MAKE(emphasis, BUF2, int)
RENDFUNC_MAKE(image, BUF4, int)
RENDFUNC_MAKE(linebreak, BUF1, int)
RENDFUNC_MAKE(link, BUF4, int)
RENDFUNC_MAKE(raw_html_tag, BUF2, int)
RENDFUNC_MAKE(triple_emphasis, BUF2, int)
RENDFUNC_MAKE(strikethrough, BUF2, int)
RENDFUNC_MAKE(superscript, BUF2, int)
RENDFUNC_MAKE(entity, BUF2, void)
RENDFUNC_MAKE(normal_text, BUF2, void)
RENDFUNC_MAKE(doc_header, BUF1, void)
RENDFUNC_MAKE(doc_footer, BUF1, void)
}
NODE_DEF_TYPE("Renderer") {
RENDFUNC_V8_DEF("blockcode", blockcode)
RENDFUNC_V8_DEF("blockquote", blockquote)
RENDFUNC_V8_DEF("blockhtml", blockhtml)
RENDFUNC_V8_DEF("header", header)
RENDFUNC_V8_DEF("hrule", hrule)
RENDFUNC_V8_DEF("list", list)
RENDFUNC_V8_DEF("listitem", listitem)
RENDFUNC_V8_DEF("paragraph", paragraph)
RENDFUNC_V8_DEF("table", table)
RENDFUNC_V8_DEF("table_row", table_row)
RENDFUNC_V8_DEF("table_cell", table_cell)
RENDFUNC_V8_DEF("autolink", autolink)
RENDFUNC_V8_DEF("codespan", codespan)
RENDFUNC_V8_DEF("double_emphasis", double_emphasis)
RENDFUNC_V8_DEF("emphasis", emphasis)
RENDFUNC_V8_DEF("image", image)
RENDFUNC_V8_DEF("linebreak", linebreak)
RENDFUNC_V8_DEF("link", link)
RENDFUNC_V8_DEF("raw_html_tag", raw_html_tag)
RENDFUNC_V8_DEF("triple_emphasis", triple_emphasis)
RENDFUNC_V8_DEF("strikethrough", strikethrough)
RENDFUNC_V8_DEF("superscript", superscript)
RENDFUNC_V8_DEF("entity", entity)
RENDFUNC_V8_DEF("normal_text", normal_text)
RENDFUNC_V8_DEF("doc_header", doc_header)
RENDFUNC_V8_DEF("doc_footer", doc_footer)
StoreTemplate("robotskirt::RendererWrap", prot);
} NODE_DEF_TYPE_END()
protected:
void wrapRenderer(sd_callbacks* cb, RendFuncData* opaque) {
RENDFUNC_WRAP(blockcode, BUF3, void)
RENDFUNC_WRAP(blockquote, BUF2, void)
RENDFUNC_WRAP(blockhtml, BUF2, void)
RENDFUNC_WRAP(header, BUF2INT, void)
RENDFUNC_WRAP(hrule, BUF1, void)
RENDFUNC_WRAP(list, BUF2INT, void)
RENDFUNC_WRAP(listitem, BUF2INT, void)
RENDFUNC_WRAP(paragraph, BUF2, void)
RENDFUNC_WRAP(table, BUF3, void)
RENDFUNC_WRAP(table_row, BUF2, void)
RENDFUNC_WRAP(table_cell, BUF2INT, void)
RENDFUNC_WRAP(autolink, BUF2INT, int)
RENDFUNC_WRAP(codespan, BUF2, int)
RENDFUNC_WRAP(double_emphasis, BUF2, int)
RENDFUNC_WRAP(emphasis, BUF2, int)
RENDFUNC_WRAP(image, BUF4, int)
RENDFUNC_WRAP(linebreak, BUF1, int)
RENDFUNC_WRAP(link, BUF4, int)
RENDFUNC_WRAP(raw_html_tag, BUF2, int)
RENDFUNC_WRAP(triple_emphasis, BUF2, int)
RENDFUNC_WRAP(strikethrough, BUF2, int)
RENDFUNC_WRAP(superscript, BUF2, int)
RENDFUNC_WRAP(entity, BUF2, void)
RENDFUNC_WRAP(normal_text, BUF2, void)
RENDFUNC_WRAP(doc_header, BUF1, void)
RENDFUNC_WRAP(doc_footer, BUF1, void)
}
// Renderer functions
RENDFUNC_DEF(blockcode, BUF3, void)
RENDFUNC_DEF(blockquote, BUF2, void)
RENDFUNC_DEF(blockhtml, BUF2, void)
RENDFUNC_DEF(header, BUF2INT, void)
RENDFUNC_DEF(hrule, BUF1, void)
RENDFUNC_DEF(list, BUF2INT, void)
RENDFUNC_DEF(listitem, BUF2INT, void)
RENDFUNC_DEF(paragraph, BUF2, void)
RENDFUNC_DEF(table, BUF3, void)
RENDFUNC_DEF(table_row, BUF2, void)
RENDFUNC_DEF(table_cell, BUF2INT, void)
RENDFUNC_DEF(autolink, BUF2INT, int)
RENDFUNC_DEF(codespan, BUF2, int)
RENDFUNC_DEF(double_emphasis, BUF2, int)
RENDFUNC_DEF(emphasis, BUF2, int)
RENDFUNC_DEF(image, BUF4, int)
RENDFUNC_DEF(linebreak, BUF1, int)
RENDFUNC_DEF(link, BUF4, int)
RENDFUNC_DEF(raw_html_tag, BUF2, int)
RENDFUNC_DEF(triple_emphasis, BUF2, int)
RENDFUNC_DEF(strikethrough, BUF2, int)
RENDFUNC_DEF(superscript, BUF2, int)
RENDFUNC_DEF(entity, BUF2, void)
RENDFUNC_DEF(normal_text, BUF2, void)
RENDFUNC_DEF(doc_header, BUF1, void)
RENDFUNC_DEF(doc_footer, BUF1, void)
};
////////////////////////////////////////////////////////////////////////////////
// SUNDOWN BUNDLED RENDERERS ([X]HTML)
////////////////////////////////////////////////////////////////////////////////
class HtmlRendererWrap: public RendererWrap {
public:
V8_CL_WRAPPER("robotskirt::HtmlRendererWrap")
HtmlRendererWrap(unsigned int flags): data(new HtmlRendFuncData) {
//FIXME:expose options (Read-only)
sd_callbacks cb;
sdhtml_renderer(&cb, (html_renderopt*)data->ptr(), flags);
wrapRenderer(&cb, data);
}
~HtmlRendererWrap() {
data->unref();
}
V8_CL_CTOR(HtmlRendererWrap) {
//Extract arguments
unsigned int flags = 0;
if (args.Length() >= 1) {
flags = CheckUFlags(args[0]);
}
inst = new HtmlRendererWrap(flags);
} V8_CL_CTOR_END()
V8_CL_GETTER(HtmlRendererWrap, Flags) {
return scope.Close(Uint(((html_renderopt*)inst->data->ptr())->flags));
} V8_GETTER_END()
NODE_DEF_TYPE("HtmlRenderer") {
V8_INHERIT("robotskirt::RendererWrap");
V8_DEF_RPROP(Flags, "flags");
StoreTemplate("robotskirt::HtmlRendererWrap", prot);
} NODE_DEF_TYPE_END()
protected:
HtmlRendFuncData* const data;
};
////////////////////////////////////////////////////////////////////////////////
// MARKDOWN CLASS DECLARATION
////////////////////////////////////////////////////////////////////////////////
//Forward the "constructor" at the subclasses
inline Markdown* newMarkdownWrap(RendererWrap* renderer, unsigned int extensions, size_t max_nesting);
inline Markdown* newStdMarkdown(unsigned int extensions, unsigned int htmlflags, size_t max_nesting);
//Base Markdown class, doesn't contain logic to store renderer data;
//this is specific to subclasses
class Markdown: public ObjectWrap {
public:
V8_CL_WRAPPER("robotskirt::Markdown")
//Here, it's important that the destructor gets declared virtual
virtual ~Markdown() {
sd_markdown_free(markdown);
}
V8_CL_CTOR(Markdown) {
//Check & extract arguments
CheckArguments(1, args);
if (!args[0]->IsObject()) V8_THROW(TypeErr("You must provide a Renderer!"));
Local<Object> obj = Obj(args[0]);
if (!GetTemplate("robotskirt::RendererWrap")->HasInstance(obj))
V8_THROW(TypeErr("You must provide a Renderer!"));
RendererWrap* rend = Unwrap<RendererWrap>(obj);
unsigned int extensions = 0;
size_t max_nesting = DEFAULT_MAX_NESTING;
if (args.Length()>=2) {
extensions = CheckUFlags(args[1]);
if (args.Length()>=3) {
max_nesting = Uint(args[2]);
}
}
inst = newMarkdownWrap(rend, extensions, max_nesting);
} V8_CL_CTOR_END()
static V8_CALLBACK(MakeStandard) {
unsigned int extensions = 0;
unsigned int htmlflags = 0;
size_t max_nesting = DEFAULT_MAX_NESTING;
if (args.Length()>=1) {
extensions = CheckUFlags(args[0]);
if (args.Length()>=2) {
htmlflags = CheckUFlags(args[1]);
if (args.Length()>=3) {
max_nesting = Uint(args[2]);
}
}
}
return newStdMarkdown(extensions, htmlflags, max_nesting)->Wrapped();
} V8_CALLBACK_END()
V8_CL_GETTER(Markdown, MaxNesting) {
return scope.Close(Uint(inst->max_nesting_));
} V8_GETTER_END()
V8_CL_GETTER(Markdown, Extensions) {
return scope.Close(Uint(inst->extensions_));
} V8_GETTER_END()
static V8_S_CALLBACK(RenderSync) {
fprintf(stderr, "renderSync() is DEPRECATED, please use render() instead.\n");
return Render(args);
}
//And the most important function(s)...
V8_CL_CALLBACK(Markdown, Render) {
//Extract input
CheckArguments(1, args);
String::Utf8Value input (args[0]);
//Prepare
BufWrap out (bufnew(OUTPUT_UNIT));
//GO!!
sd_markdown_render(*out,
reinterpret_cast<const unsigned char*>(*input),
input.length(),
inst->markdown);
//Finish
return scope.Close(toString(*out));
} V8_CALLBACK_END()
NODE_DEF_TYPE("Markdown") {
V8_DEF_RPROP(Extensions, "extensions");
V8_DEF_RPROP(MaxNesting, "maxNesting");
V8_DEF_METHOD(Render, "render");
V8_DEF_METHOD(RenderSync, "renderSync");
prot->GetFunction()->Set(Symbol("std"), Func(MakeStandard)->GetFunction());
StoreTemplate("robotskirt::Markdown", prot);
} NODE_DEF_TYPE_END()
protected:
sd_markdown* markdown;
sd_callbacks cb;
size_t max_nesting_;
int extensions_;
};
// A markdown parser holding JS-wrapped Renderer data.
class MarkdownWrap : public Markdown {
public:
MarkdownWrap(RendererWrap* renderer, unsigned int extensions, size_t max_nesting) {
max_nesting_ = max_nesting;
extensions_ = extensions;
markdown = makeMarkdown(renderer, &cb, &opaque, extensions, max_nesting);
}
//FIXME: is deallocation correct?
protected:
RendererData opaque;
private:
static inline sd_markdown* makeMarkdown(RendererWrap* rend,
sd_callbacks* cb, RendererData* opaque,
unsigned int extensions, size_t max_nesting) {
rend->makeRenderer(cb, opaque);
return sd_markdown_new(extensions, max_nesting, cb, opaque);
}
};
// A markdown parser holding a standard Sundown HTML renderer
class StdMarkdown : public Markdown {
public:
StdMarkdown(unsigned int extensions, unsigned int htmlflags, size_t max_nesting) {
max_nesting_ = max_nesting;
extensions_ = extensions;
//Create HTML renderer
sdhtml_renderer(&cb, &options, htmlflags);
//Create the Markdown parser
markdown = sd_markdown_new(extensions, max_nesting, &cb, &options);
}
protected:
html_renderopt options;
};
//Now we can define the "constructors"
inline Markdown* newMarkdownWrap(RendererWrap* renderer, unsigned int extensions, size_t max_nesting) {
return new MarkdownWrap(renderer, extensions, max_nesting);
}
inline Markdown* newStdMarkdown(unsigned int extensions, unsigned int htmlflags, size_t max_nesting) {
return new StdMarkdown(extensions, htmlflags, max_nesting);
}
////////////////////////////////////////////////////////////////////////////////
// HTML Renderer options
////////////////////////////////////////////////////////////////////////////////
//class TocData: public ObjectWrap {
//private:
// html_renderopt* handle;
// int current_level_, level_offset_;
//public:
// int getHeaderCount() const {return handle.header_count;}
// int getCurrentLevel() const {return current_level_;}
// int getLevelOffset() const {return level_offset_;}
//
// void setHeaderCount(int header_count) {handle.header_count=header_count;}
// void setCurrentLevel(int current_level) {current_level_ = current_level;}
// void setLevelOffset(int level_offset) {level_offset_ = level_offset;}
//
// TocData() {}
// TocData(int header_count, int current_level, int level_offset) {
// handle.header_count = header_count;
// handle.current_level = current_level;
// handle.level_offset = level_offset;
// }
// TocData(TocData& other) : handle(other.handle) {}
// ~TocData() {}
//
// static V8_CALLBACK(New, 0) {
// int arg0 = 0;
// int arg1 = 0;
// int arg2 = 0;
//
// //Extract arguments
// if (args.Length() >= 1) {
// arg0 = CheckInt(args[0]);
// if (args.Length() >= 2) {
// arg1 = CheckInt(args[1]);
// if (args.Length() >= 3) {
// arg2 = CheckInt(args[2]);
// }
// }
// }
//
// (new TocData(arg0, arg1, arg2))->Wrap(args.This());
// return args.This();
// } V8_WRAP_END()
//
// //Getters
// static V8_GETTER(GetHeaderCount) {
// TocData& h = *(Unwrap<TocData>(info.Holder()));
// return scope.Close(Integer::New(h.handle.header_count));
// } V8_WRAP_END()
// static V8_GETTER(GetCurrentLevel) {
// TocData& h = *(Unwrap<TocData>(info.Holder()));
// return scope.Close(Integer::New(h.current_level_));
// } V8_WRAP_END()
// static V8_GETTER(GetLevelOffset) {
// TocData& h = *(Unwrap<TocData>(info.Holder()));
// return scope.Close(Integer::New(h.level_offset_));
// } V8_WRAP_END()
//
// //Setters
// static V8_SETTER(SetHeaderCount) {
// TocData& h = *(Unwrap<TocData>(info.Holder()));
// h.handle.header_count = CheckInt(value);
// } V8_WRAP_END_NR()
// static V8_SETTER(SetCurrentLevel) {
// TocData& h = *(Unwrap<TocData>(info.Holder()));
// h.current_level_ = CheckInt(value);
// } V8_WRAP_END_NR()
// static V8_SETTER(SetLevelOffset) {
// TocData& h = *(Unwrap<TocData>(info.Holder()));
// h.level_offset_ = CheckInt(value);
// } V8_WRAP_END_NR()
//};
//class HtmlOptions: public ObjectWrap {
//private:
// unsigned int flags = 0;
//public:
//
//};
////////////////////////////////////////////////////////////////////////////////
// OTHER UTILITIES (version stuff, complementary functionality, etc.)
////////////////////////////////////////////////////////////////////////////////
Local<Object> SundownVersion() {
return (new Version(SUNDOWN_VER_MAJOR,
SUNDOWN_VER_MINOR,
SUNDOWN_VER_REVISION))->Wrapped();
}
//SUBMODULE: Houdini, the escapist (a part comes bundled with Sundown)
//NOTE: it also UNescapes, but that breaks the joke in the name so nevermind.
namespace houdini {
#define HOUDINI_OUTPUT_UNIT OUTPUT_UNIT
#define HOUDINI_DEFAULT_TO_SECURE true
#define HOUDINI_STANDARD_WRAPPER(IDENTIFIER, FUNCTION) \
V8_S_CALLBACK(IDENTIFIER) { \