Skip to content
Myr

Merge branch 'dev' of https://github.com/nothings/stb into dev

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parents a32aeef, dcb1116 · Browse files · 18 files+182 added−1425 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

18 files+182 added−1425 removed

  • tests:+0 added−0 removed
  • other:+182 added−1425 removed

18 files+182 added−1425 removed

This commit is too large to show whole: its files are listed by path, without their changes.

added:.github/workflows/ci-fuzz.yml+23 added
1name: CIFuzz2on: [pull_request]3jobs:4 Fuzzing:5 runs-on: ubuntu-latest6 steps:7 - name: Build Fuzzers8 uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master9 with:10 oss-fuzz-project-name: 'stb'11 dry-run: false12 - name: Run Fuzzers13 uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master14 with:15 oss-fuzz-project-name: 'stb'16 fuzz-seconds: 90017 dry-run: false18 - name: Upload Crash19 uses: actions/upload-artifact@v120 if: failure()21 with:22 name: artifacts23 path: ./out/artifacts
modified:deprecated/stb.h+7 added-1357 removed
6130 unmodified lines
6131 char **results = NULL;6132 char buffer[4096], with_slash[4096];6133 size_t n;61346135 #ifdef _MSC_VER6135 #ifdef WIN326136 stb__wchar *ws;6137 struct _wfinddata_t data;6138 #ifdef _WIN646139 const intptr_t none = -1;
1 unmodified line
6141 #else6142 const long none = -1;6143 long z;6144 #endif6145 #else // !_MSC_VER6145 #else // !WIN326146 const DIR *none = NULL;6147 DIR *z;6148 #endif6149
8 unmodified lines
6158 buffer[n] = 0;6159 if (!stb_strscpy(with_slash,buffer,sizeof(with_slash)))6160 return NULL;61616162 #ifdef _MSC_VER6162 #ifdef WIN326163 if (!stb_strscpy(buffer+n,"*.*",sizeof(buffer)-n))6164 return NULL;6165 ws = stb__from_utf8(buffer);6166 z = _wfindfirst((wchar_t *)ws, &data);
2 unmodified lines
6169 #endif61706171 if (z != none) {6172 int nonempty = STB_TRUE;6173 #ifndef _MSC_VER6173 #ifndef WIN326174 struct dirent *data = readdir(z);6175 nonempty = (data != NULL);6176 #endif61776178 if (nonempty) {61796180 do {6181 int is_subdir;6182 #ifdef _MSC_VER6182 #ifdef WIN326183 char *name = stb__to_utf8((stb__wchar *)data.name);6184 if (name == NULL) {6185 fprintf(stderr, "%s to convert '%S' to %s!\n", "Unable", data.name, "utf8");6186 continue;
19 unmodified lines
6206 }6207 }6208 }6209 }6210 #ifdef _MSC_VER6210 #ifdef WIN326211 while (0 == _wfindnext(z, &data));6212 #else6213 while ((data = readdir(z)) != NULL);6214 #endif6215 }6216 #ifdef _MSC_VER6216 #ifdef WIN326217 _findclose(z);6218 #else6219 closedir(z);6220 #endif
6837 unmodified lines
1305813059#endif // STB_DEFINE13060#endif // !STB_DEBUG && !STB_ALWAYS_H130611306213063#ifdef STB_STUA13064#error "STUA is no longer supported"13065//////////////////////////////////////////////////////////////////////////13066//13067// stua: little scripting language13068//13069// define STB_STUA to compile it13070//13071// see http://nothings.org/stb/stb_stua.html for documentation13072//13073// basic parsing model:13074//13075// lexical analysis13076// use stb_lex() to parse tokens; keywords get their own tokens13077//13078// parsing:13079// recursive descent parser. too much of a hassle to make an unambiguous13080// LR(1) grammar, and one-pass generation is clumsier (recursive descent13081// makes it easier to e.g. compile nested functions). on the other hand,13082// dictionary syntax required hackery to get extra lookahead.13083//13084// codegen:13085// output into an evaluation tree, using array indices as 'pointers'13086//13087// run:13088// traverse the tree; support for 'break/continue/return' is tricky13089//13090// garbage collection:13091// stu__mark and sweep; explicit stack with non-stu__compile_global_scope roots1309213093typedef stb_int32 stua_obj;1309413095typedef stb_idict stua_dict;1309613097STB_EXTERN void stua_run_script(char *s);13098STB_EXTERN void stua_uninit(void);1309913100extern stua_obj stua_globals;1310113102STB_EXTERN double stua_number(stua_obj z);1310313104STB_EXTERN stua_obj stua_getnil(void);13105STB_EXTERN stua_obj stua_getfalse(void);13106STB_EXTERN stua_obj stua_gettrue(void);13107STB_EXTERN stua_obj stua_string(char *z);13108STB_EXTERN stua_obj stua_make_number(double d);13109STB_EXTERN stua_obj stua_box(int type, void *data, int size);1311013111enum13112{13113 STUA_op_negate=129,13114 STUA_op_shl, STUA_op_ge,13115 STUA_op_shr, STUA_op_le,13116 STUA_op_shru,13117 STUA_op_last13118};1311913120#define STUA_NO_VALUE 2 // equivalent to a tagged NULL13121STB_EXTERN stua_obj (*stua_overload)(int op, stua_obj a, stua_obj b, stua_obj c);1312213123STB_EXTERN stua_obj stua_error(char *err, ...);1312413125STB_EXTERN stua_obj stua_pushroot(stua_obj o);13126STB_EXTERN void stua_poproot ( void );131271312813129#ifdef STB_DEFINE13130// INTERPRETER1313113132// 31-bit floating point implementation13133// force the (1 << 30) bit (2nd highest bit) to be zero by re-biasing the exponent;13134// then shift and set the bottom bit1313513136static stua_obj stu__floatp(float *f)13137{13138 unsigned int n = *(unsigned int *) f;13139 unsigned int e = n & (0xff << 23);1314013141 assert(sizeof(int) == 4 && sizeof(float) == 4);1314213143 if (!e) // zero?13144 n = n; // no change13145 else if (e < (64 << 23)) // underflow of the packed encoding?13146 n = (n & 0x80000000); // signed 013147 else if (e > (190 << 23)) // overflow of the encoding? (or INF or NAN)13148 n = (n & 0x80000000) + (127 << 23); // new INF encoding13149 else13150 n -= 0x20000000;1315113152 // now we need to shuffle the bits so that the spare bit is at the bottom13153 assert((n & 0x40000000) == 0);13154 return (n & 0x80000000) + (n << 1) + 1;13155}1315613157static unsigned char stu__getfloat_addend[256];13158static float stu__getfloat(stua_obj v)13159{13160 unsigned int n;13161 unsigned int e = ((unsigned int) v) >> 24;1316213163 n = (int) v >> 1; // preserve high bit13164 n += stu__getfloat_addend[e] << 24;13165 return *(float *) &n;13166}1316713168stua_obj stua_float(float f)13169{13170 return stu__floatp(&f);13171}1317213173static void stu__float_init(void)13174{13175 int i;13176 stu__getfloat_addend[0] = 0; // do nothing to biased exponent of 013177 for (i=1; i < 127; ++i)13178 stu__getfloat_addend[i] = 32; // undo the -0x2000000013179 stu__getfloat_addend[127] = 64; // convert packed INF to INF (0x3f -> 0x7f)1318013181 for (i=0; i < 128; ++i) // for signed floats, remove the bit we just shifted down13182 stu__getfloat_addend[128+i] = stu__getfloat_addend[i] - 64;13183}1318413185// Tagged data type implementation1318613187 // TAGS:13188#define stu__int_tag 0 // of 2 bits // 00 int13189#define stu__float_tag 1 // of 1 bit // 01 float13190#define stu__ptr_tag 2 // of 2 bits // 10 boxed13191 // 11 float1319213193#define stu__tag(x) ((x) & 3)13194#define stu__number(x) (stu__tag(x) != stu__ptr_tag)13195#define stu__isint(x) (stu__tag(x) == stu__int_tag)1319613197#define stu__int(x) ((x) >> 2)13198#define stu__float(x) (stu__getfloat(x))1319913200#define stu__makeint(v) ((v)*4+stu__int_tag)1320113202// boxed data, and tag support for boxed data1320313204enum13205{13206 STU___float = 1, STU___int = 2,13207 STU___number = 3, STU___string = 4,13208 STU___function = 5, STU___dict = 6,13209 STU___boolean = 7, STU___error = 8,13210};1321113212// boxed data13213#define STU__BOX short type, stua_gc13214typedef struct stu__box { STU__BOX; } stu__box;1321513216stu__box stu__nil = { 0, 1 };13217stu__box stu__true = { STU___boolean, 1, };13218stu__box stu__false = { STU___boolean, 1, };1321913220#define stu__makeptr(v) ((stua_obj) (v) + stu__ptr_tag)1322113222#define stua_nil stu__makeptr(&stu__nil)13223#define stua_true stu__makeptr(&stu__true)13224#define stua_false stu__makeptr(&stu__false)1322513226stua_obj stua_getnil(void) { return stua_nil; }13227stua_obj stua_getfalse(void) { return stua_false; }13228stua_obj stua_gettrue(void) { return stua_true; }1322913230#define stu__ptr(x) ((stu__box *) ((x) - stu__ptr_tag))1323113232#define stu__checkt(t,x) ((t) == STU___float ? ((x) & 1) == stu__float_tag : \13233 (t) == STU___int ? stu__isint(x) : \13234 (t) == STU___number ? stu__number(x) : \13235 stu__tag(x) == stu__ptr_tag && stu__ptr(x)->type == (t))1323613237typedef struct13238{13239 STU__BOX;13240 void *ptr;13241} stu__wrapper;1324213243// implementation of a 'function' or function + closure1324413245typedef struct stu__func13246{13247 STU__BOX;13248 stua_obj closure_source; // 0 - regular function; 4 - C function13249 // if closure, pointer to source function13250 union {13251 stua_obj closure_data; // partial-application data13252 void *store; // pointer to free that holds 'code'13253 stua_obj (*func)(stua_dict *context);13254 } f;13255 // closure ends here13256 short *code;13257 int num_param;13258 stua_obj *param; // list of parameter strings13259} stu__func;1326013261// apply this to 'short *code' to get at data13262#define stu__const(f) ((stua_obj *) (f))1326313264static void stu__free_func(stu__func *f)13265{13266 if (f->closure_source == 0) free(f->f.store);13267 if ((stb_uint) f->closure_source <= 4) free(f->param);13268 free(f);13269}1327013271#define stu__pd(x) ((stua_dict *) stu__ptr(x))13272#define stu__pw(x) ((stu__wrapper *) stu__ptr(x))13273#define stu__pf(x) ((stu__func *) stu__ptr(x))132741327513276// garbage-collection132771327813279static stu__box ** stu__gc_ptrlist;13280static stua_obj * stu__gc_root_stack;1328113282stua_obj stua_pushroot(stua_obj o) { stb_arr_push(stu__gc_root_stack, o); return o; }13283void stua_poproot ( void ) { stb_arr_pop(stu__gc_root_stack); }1328413285static stb_sdict *stu__strings;13286static void stu__mark(stua_obj z)13287{13288 int i;13289 stu__box *p = stu__ptr(z);13290 if (p->stua_gc == 1) return; // already marked13291 assert(p->stua_gc == 0);13292 p->stua_gc = 1;13293 switch(p->type) {13294 case STU___function: {13295 stu__func *f = (stu__func *) p;13296 if ((stb_uint) f->closure_source <= 4) {13297 if (f->closure_source == 0) {13298 for (i=1; i <= f->code[0]; ++i)13299 if (!stu__number(((stua_obj *) f->code)[-i]))13300 stu__mark(((stua_obj *) f->code)[-i]);13301 }13302 for (i=0; i < f->num_param; ++i)13303 stu__mark(f->param[i]);13304 } else {13305 stu__mark(f->closure_source);13306 stu__mark(f->f.closure_data);13307 }13308 break;13309 }13310 case STU___dict: {13311 stua_dict *e = (stua_dict *) p;13312 for (i=0; i < e->limit; ++i)13313 if (e->table[i].k != STB_IEMPTY && e->table[i].k != STB_IDEL) {13314 if (!stu__number(e->table[i].k)) stu__mark((int) e->table[i].k);13315 if (!stu__number(e->table[i].v)) stu__mark((int) e->table[i].v);13316 }13317 break;13318 }13319 }13320}1332113322static int stu__num_allocs, stu__size_allocs;13323static stua_obj stu__flow_val = stua_nil; // used for break & return1332413325static void stua_gc(int force)13326{13327 int i;13328 if (!force && stu__num_allocs == 0 && stu__size_allocs == 0) return;13329 stu__num_allocs = stu__size_allocs = 0;13330 //printf("[gc]\n");1333113332 // clear marks13333 for (i=0; i < stb_arr_len(stu__gc_ptrlist); ++i)13334 stu__gc_ptrlist[i]->stua_gc = 0;1333513336 // stu__mark everything reachable13337 stu__nil.stua_gc = stu__true.stua_gc = stu__false.stua_gc = 1;13338 stu__mark(stua_globals);13339 if (!stu__number(stu__flow_val))13340 stu__mark(stu__flow_val);13341 for (i=0; i < stb_arr_len(stu__gc_root_stack); ++i)13342 if (!stu__number(stu__gc_root_stack[i]))13343 stu__mark(stu__gc_root_stack[i]);1334413345 // sweep unreachables13346 for (i=0; i < stb_arr_len(stu__gc_ptrlist);) {13347 stu__box *z = stu__gc_ptrlist[i];13348 if (!z->stua_gc) {13349 switch (z->type) {13350 case STU___dict: stb_idict_destroy((stua_dict *) z); break;13351 case STU___error: free(((stu__wrapper *) z)->ptr); break;13352 case STU___string: stb_sdict_remove(stu__strings, (char*) ((stu__wrapper *) z)->ptr, NULL); free(z); break;13353 case STU___function: stu__free_func((stu__func *) z); break;13354 }13355 // swap in the last item over this, and repeat13356 z = stb_arr_pop(stu__gc_ptrlist);13357 stu__gc_ptrlist[i] = z;13358 } else13359 ++i;13360 }13361}1336213363static void stu__consider_gc(stua_obj x)13364{13365 if (stu__size_allocs < 100000) return;13366 if (stu__num_allocs < 10 && stu__size_allocs < 1000000) return;13367 stb_arr_push(stu__gc_root_stack, x);13368 stua_gc(0);13369 stb_arr_pop(stu__gc_root_stack);13370}1337113372static stua_obj stu__makeobj(int type, void *data, int size, int safe_to_gc)13373{13374 stua_obj x = stu__makeptr(data);13375 ((stu__box *) data)->type = type;13376 stb_arr_push(stu__gc_ptrlist, (stu__box *) data);13377 stu__num_allocs += 1;13378 stu__size_allocs += size;13379 if (safe_to_gc) stu__consider_gc(x);13380 return x;13381}1338213383stua_obj stua_box(int type, void *data, int size)13384{13385 stu__wrapper *p = (stu__wrapper *) malloc(sizeof(*p));13386 p->ptr = data;13387 return stu__makeobj(type, p, size, 0);13388}1338913390// a stu string can be directly compared for equality, because13391// they go into a hash table13392stua_obj stua_string(char *z)13393{13394 stu__wrapper *b = (stu__wrapper *) stb_sdict_get(stu__strings, z);13395 if (b == NULL) {13396 int o = stua_box(STU___string, NULL, strlen(z) + sizeof(*b));13397 b = stu__pw(o);13398 stb_sdict_add(stu__strings, z, b);13399 stb_sdict_getkey(stu__strings, z, (char **) &b->ptr);13400 }13401 return stu__makeptr(b);13402}1340313404// stb_obj dictionary is just an stb_idict13405static void stu__set(stua_dict *d, stua_obj k, stua_obj v)13406{ if (stb_idict_set(d, k, v)) stu__size_allocs += 8; }1340713408static stua_obj stu__get(stua_dict *d, stua_obj k, stua_obj res)13409{13410 stb_idict_get_flag(d, k, &res);13411 return res;13412}1341313414static stua_obj make_string(char *z, int len)13415{13416 stua_obj s;13417 char temp[256], *q = (char *) stb_temp(temp, len+1), *p = q;13418 while (len > 0) {13419 if (*z == '\\') {13420 if (z[1] == 'n') *p = '\n';13421 else if (z[1] == 'r') *p = '\r';13422 else if (z[1] == 't') *p = '\t';13423 else *p = z[1];13424 p += 1; z += 2; len -= 2;13425 } else {13426 *p++ = *z++; len -= 1;13427 }13428 }13429 *p = 0;13430 s = stua_string(q);13431 stb_tempfree(temp, q);13432 return s;13433}1343413435enum token_names13436{13437 T__none=128,13438 ST_shl = STUA_op_shl, ST_ge = STUA_op_ge,13439 ST_shr = STUA_op_shr, ST_le = STUA_op_le,13440 ST_shru = STUA_op_shru, STU__negate = STUA_op_negate,13441 ST__reset_numbering = STUA_op_last,13442 ST_white,13443 ST_id, ST_float, ST_decimal, ST_hex, ST_char,ST_string, ST_number,13444 // make sure the keywords come _AFTER_ ST_id, so stb_lex prefer them13445 ST_if, ST_while, ST_for, ST_eq, ST_nil,13446 ST_then, ST_do, ST_in, ST_ne, ST_true,13447 ST_else, ST_break, ST_let, ST_and, ST_false,13448 ST_elseif, ST_continue, ST_into, ST_or, ST_repeat,13449 ST_end, ST_as, ST_return, ST_var, ST_func,13450 ST_catch, ST__frame,13451 ST__max_terminals,1345213453 STU__defaultparm, STU__seq,13454};1345513456static stua_dict * stu__globaldict;13457 stua_obj stua_globals;1345813459static enum13460{13461 FLOW_normal, FLOW_continue, FLOW_break, FLOW_return, FLOW_error,13462} stu__flow;1346313464stua_obj stua_error(char *z, ...)13465{13466 stua_obj a;13467 char temp[4096], *x;13468 va_list v; va_start(v,z); vsprintf(temp, z, v); va_end(v);13469 x = stb_p_strdup(temp);13470 a = stua_box(STU___error, x, strlen(x));13471 stu__flow = FLOW_error;13472 stu__flow_val = a;13473 return stua_nil;13474}1347513476double stua_number(stua_obj z)13477{13478 return stu__tag(z) == stu__int_tag ? stu__int(z) : stu__float(z);13479}1348013481stua_obj stua_make_number(double d)13482{13483 double e = floor(d);13484 if (e == d && e < (1 << 29) && e >= -(1 << 29))13485 return stu__makeint((int) e);13486 else13487 return stua_float((float) d);13488}1348913490stua_obj (*stua_overload)(int op, stua_obj a, stua_obj b, stua_obj c) = NULL;1349113492static stua_obj stu__op(int op, stua_obj a, stua_obj b, stua_obj c)13493{13494 stua_obj r = STUA_NO_VALUE;13495 if (op == '+') {13496 if (stu__checkt(STU___string, a) && stu__checkt(STU___string, b)) {13497 ;// @TODO: string concatenation13498 } else if (stu__checkt(STU___function, a) && stu__checkt(STU___dict, b)) {13499 stu__func *f = (stu__func *) malloc(12);13500 assert(offsetof(stu__func, code)==12);13501 f->closure_source = a;13502 f->f.closure_data = b;13503 return stu__makeobj(STU___function, f, 16, 1);13504 }13505 }13506 if (stua_overload) r = stua_overload(op,a,b,c);13507 if (stu__flow != FLOW_error && r == STUA_NO_VALUE)13508 stua_error("Typecheck for operator %d", op), r=stua_nil;13509 return r;13510}1351113512#define STU__EVAL2(a,b) \13513 a = stu__eval(stu__f[n+1]); if (stu__flow) break; stua_pushroot(a); \13514 b = stu__eval(stu__f[n+2]); stua_poproot(); if (stu__flow) break;1351513516#define STU__FB(op) \13517 STU__EVAL2(a,b) \13518 if (stu__tag(a) == stu__int_tag && stu__tag(b) == stu__int_tag) \13519 return ((a) op (b)); \13520 if (stu__number(a) && stu__number(b)) \13521 return stua_make_number(stua_number(a) op stua_number(b)); \13522 return stu__op(stu__f[n], a,b, stua_nil)1352313524#define STU__F(op) \13525 STU__EVAL2(a,b) \13526 if (stu__number(a) && stu__number(b)) \13527 return stua_make_number(stua_number(a) op stua_number(b)); \13528 return stu__op(stu__f[n], a,b, stua_nil)1352913530#define STU__I(op) \13531 STU__EVAL2(a,b) \13532 if (stu__tag(a) == stu__int_tag && stu__tag(b) == stu__int_tag) \13533 return stu__makeint(stu__int(a) op stu__int(b)); \13534 return stu__op(stu__f[n], a,b, stua_nil)1353513536#define STU__C(op) \13537 STU__EVAL2(a,b) \13538 if (stu__number(a) && stu__number(b)) \13539 return (stua_number(a) op stua_number(b)) ? stua_true : stua_false; \13540 return stu__op(stu__f[n], a,b, stua_nil)1354113542#define STU__CE(op) \13543 STU__EVAL2(a,b) \13544 return (a op b) ? stua_true : stua_false1354513546static short *stu__f;13547static stua_obj stu__f_obj;13548static stua_dict *stu__c;13549static stua_obj stu__funceval(stua_obj fo, stua_obj co);1355013551static int stu__cond(stua_obj x)13552{13553 if (stu__flow) return 0;13554 if (!stu__checkt(STU___boolean, x))13555 x = stu__op('!', x, stua_nil, stua_nil);13556 if (x == stua_true ) return 1;13557 if (x == stua_false) return 0;13558 stu__flow = FLOW_error;13559 return 0;13560}1356113562// had to manually eliminate tailcall recursion for debugging complex stuff13563#define TAILCALL(x) n = (x); goto top;13564static stua_obj stu__eval(int n)13565{13566top:13567 if (stu__flow >= FLOW_return) return stua_nil; // is this needed?13568 if (n < 0) return stu__const(stu__f)[n];13569 assert(n != 0 && n != 1);13570 switch (stu__f[n]) {13571 stua_obj a,b,c;13572 case ST_catch: a = stu__eval(stu__f[n+1]);13573 if (stu__flow == FLOW_error) { a=stu__flow_val; stu__flow = FLOW_normal; }13574 return a;13575 case ST_var: b = stu__eval(stu__f[n+2]); if (stu__flow) break;13576 stu__set(stu__c, stu__const(stu__f)[stu__f[n+1]], b);13577 return b;13578 case STU__seq: stu__eval(stu__f[n+1]); if (stu__flow) break;13579 TAILCALL(stu__f[n+2]);13580 case ST_if: if (!stu__cond(stu__eval(stu__f[n+1]))) return stua_nil;13581 TAILCALL(stu__f[n+2]);13582 case ST_else: a = stu__cond(stu__eval(stu__f[n+1]));13583 TAILCALL(stu__f[n + 2 + !a]);13584 #define STU__HANDLE_BREAK \13585 if (stu__flow >= FLOW_break) { \13586 if (stu__flow == FLOW_break) { \13587 a = stu__flow_val; \13588 stu__flow = FLOW_normal; \13589 stu__flow_val = stua_nil; \13590 return a; \13591 } \13592 return stua_nil; \13593 }13594 case ST_as: stu__eval(stu__f[n+3]);13595 STU__HANDLE_BREAK13596 // fallthrough!13597 case ST_while: a = stua_nil; stua_pushroot(a);13598 while (stu__cond(stu__eval(stu__f[n+1]))) {13599 stua_poproot();13600 a = stu__eval(stu__f[n+2]);13601 STU__HANDLE_BREAK13602 stu__flow = FLOW_normal; // clear 'continue' flag13603 stua_pushroot(a);13604 if (stu__f[n+3]) stu__eval(stu__f[n+3]);13605 STU__HANDLE_BREAK13606 stu__flow = FLOW_normal; // clear 'continue' flag13607 }13608 stua_poproot();13609 return a;13610 case ST_break: stu__flow = FLOW_break; stu__flow_val = stu__eval(stu__f[n+1]); break;13611 case ST_continue:stu__flow = FLOW_continue; break;13612 case ST_return: stu__flow = FLOW_return; stu__flow_val = stu__eval(stu__f[n+1]); break;13613 case ST__frame: return stu__f_obj;13614 case '[': STU__EVAL2(a,b);13615 if (stu__checkt(STU___dict, a))13616 return stu__get(stu__pd(a), b, stua_nil);13617 return stu__op(stu__f[n], a, b, stua_nil);13618 case '=': a = stu__eval(stu__f[n+2]); if (stu__flow) break;13619 n = stu__f[n+1];13620 if (stu__f[n] == ST_id) {13621 if (!stb_idict_update(stu__c, stu__const(stu__f)[stu__f[n+1]], a))13622 if (!stb_idict_update(stu__globaldict, stu__const(stu__f)[stu__f[n+1]], a))13623 return stua_error("Assignment to undefined variable");13624 } else if (stu__f[n] == '[') {13625 stua_pushroot(a);13626 b = stu__eval(stu__f[n+1]); if (stu__flow) { stua_poproot(); break; }13627 stua_pushroot(b);13628 c = stu__eval(stu__f[n+2]); stua_poproot(); stua_poproot();13629 if (stu__flow) break;13630 if (!stu__checkt(STU___dict, b)) return stua_nil;13631 stu__set(stu__pd(b), c, a);13632 } else {13633 return stu__op(stu__f[n], stu__eval(n), a, stua_nil);13634 }13635 return a;13636 case STU__defaultparm:13637 a = stu__eval(stu__f[n+2]);13638 stu__flow = FLOW_normal;13639 if (stb_idict_add(stu__c, stu__const(stu__f)[stu__f[n+1]], a))13640 stu__size_allocs += 8;13641 return stua_nil;13642 case ST_id: a = stu__get(stu__c, stu__const(stu__f)[stu__f[n+1]], STUA_NO_VALUE); // try local variable13643 return a != STUA_NO_VALUE // else try stu__compile_global_scope variable13644 ? a : stu__get(stu__globaldict, stu__const(stu__f)[stu__f[n+1]], stua_nil);13645 case STU__negate:a = stu__eval(stu__f[n+1]); if (stu__flow) break;13646 return stu__isint(a) ? -a : stu__op(stu__f[n], a, stua_nil, stua_nil);13647 case '~': a = stu__eval(stu__f[n+1]); if (stu__flow) break;13648 return stu__isint(a) ? (~a)&~3 : stu__op(stu__f[n], a, stua_nil, stua_nil);13649 case '!': a = stu__eval(stu__f[n+1]); if (stu__flow) break;13650 a = stu__cond(a); if (stu__flow) break;13651 return a ? stua_true : stua_false;13652 case ST_eq: STU__CE(==); case ST_le: STU__C(<=); case '<': STU__C(<);13653 case ST_ne: STU__CE(!=); case ST_ge: STU__C(>=); case '>': STU__C(>);13654 case '+' : STU__FB(+); case '*': STU__F(*); case '&': STU__I(&); case ST_shl: STU__I(<<);13655 case '-' : STU__FB(-); case '/': STU__F(/); case '|': STU__I(|); case ST_shr: STU__I(>>);13656 case '%': STU__I(%); case '^': STU__I(^);13657 case ST_shru: STU__EVAL2(a,b);13658 if (stu__tag(a) == stu__int_tag && stu__tag(b) == stu__int_tag)13659 return stu__makeint((unsigned) stu__int(a) >> stu__int(b));13660 return stu__op(stu__f[n], a,b, stua_nil);13661 case ST_and: a = stu__eval(stu__f[n+1]); b = stu__cond(a); if (stu__flow) break;13662 return a ? stu__eval(stu__f[n+2]) : a;13663 case ST_or : a = stu__eval(stu__f[n+1]); b = stu__cond(a); if (stu__flow) break;13664 return a ? b : stu__eval(stu__f[n+2]);13665 case'(':case':': STU__EVAL2(a,b);13666 if (!stu__checkt(STU___function, a))13667 return stu__op(stu__f[n], a,b, stua_nil);13668 if (!stu__checkt(STU___dict, b))13669 return stua_nil;13670 if (stu__f[n] == ':')13671 b = stu__makeobj(STU___dict, stb_idict_copy(stu__pd(b)), stb_idict_memory_usage(stu__pd(b)), 0);13672 a = stu__funceval(a,b);13673 return a;13674 case '{' : {13675 stua_dict *d;13676 d = stb_idict_new_size(stu__f[n+1] > 40 ? 64 : 16);13677 if (d == NULL)13678 return stua_nil; // breakpoint fodder13679 c = stu__makeobj(STU___dict, d, 32, 1);13680 stua_pushroot(c);13681 a = stu__f[n+1];13682 for (b=0; b < a; ++b) {13683 stua_obj x = stua_pushroot(stu__eval(stu__f[n+2 + b*2 + 0]));13684 stua_obj y = stu__eval(stu__f[n+2 + b*2 + 1]);13685 stua_poproot();13686 if (stu__flow) { stua_poproot(); return stua_nil; }13687 stu__set(d, x, y);13688 }13689 stua_poproot();13690 return c;13691 }13692 default: if (stu__f[n] < 0) return stu__const(stu__f)[stu__f[n]];13693 assert(0); /* NOTREACHED */ // internal error!13694 }13695 return stua_nil;13696}1369713698int stb__stua_nesting;13699static stua_obj stu__funceval(stua_obj fo, stua_obj co)13700{13701 stu__func *f = stu__pf(fo);13702 stua_dict *context = stu__pd(co);13703 int i,j;13704 stua_obj p;13705 short *tf = stu__f; // save previous function13706 stua_dict *tc = stu__c;1370713708 if (stu__flow == FLOW_error) return stua_nil;13709 assert(stu__flow == FLOW_normal);1371013711 stua_pushroot(fo);13712 stua_pushroot(co);13713 stu__consider_gc(stua_nil);1371413715 while ((stb_uint) f->closure_source > 4) {13716 // add data from closure to context13717 stua_dict *e = (stua_dict *) stu__pd(f->f.closure_data);13718 for (i=0; i < e->limit; ++i)13719 if (e->table[i].k != STB_IEMPTY && e->table[i].k != STB_IDEL)13720 if (stb_idict_add(context, e->table[i].k, e->table[i].v))13721 stu__size_allocs += 8;13722 // use add so if it's already defined, we don't override it; that way13723 // explicit parameters win over applied ones, and most recent applications13724 // win over previous ones13725 f = stu__pf(f->closure_source);13726 }1372713728 for (j=0, i=0; i < f->num_param; ++i)13729 // if it doesn't already exist, add it from the numbered parameters13730 if (stb_idict_add(context, f->param[i], stu__get(context, stu__int(j), stua_nil)))13731 ++j;1373213733 // @TODO: if (stu__get(context, stu__int(f->num_param+1)) != STUA_NO_VALUE) // error: too many parameters13734 // @TODO: ditto too few parameters1373513736 if (f->closure_source == 4)13737 p = f->f.func(context);13738 else {13739 stu__f = f->code, stu__c = context;13740 stu__f_obj = co;13741 ++stb__stua_nesting;13742 if (stu__f[1])13743 p = stu__eval(stu__f[1]);13744 else13745 p = stua_nil;13746 --stb__stua_nesting;13747 stu__f = tf, stu__c = tc; // restore previous function13748 if (stu__flow == FLOW_return) {13749 stu__flow = FLOW_normal;13750 p = stu__flow_val;13751 stu__flow_val = stua_nil;13752 }13753 }1375413755 stua_poproot();13756 stua_poproot();1375713758 return p;13759}1376013761// Parser1376213763static int stu__tok;13764static stua_obj stu__tokval;1376513766static char *stu__curbuf, *stu__bufstart;1376713768static stb_matcher *stu__lex_matcher;1376913770static unsigned char stu__prec[ST__max_terminals], stu__end[ST__max_terminals];1377113772static void stu__nexttoken(void)13773{13774 int len;1377513776retry:13777 stu__tok = stb_lex(stu__lex_matcher, stu__curbuf, &len);13778 if (stu__tok == 0)13779 return;13780 switch(stu__tok) {13781 case ST_white : stu__curbuf += len; goto retry;13782 case T__none : stu__tok = *stu__curbuf; break;13783 case ST_string: stu__tokval = make_string(stu__curbuf+1, len-2); break;13784 case ST_id : stu__tokval = make_string(stu__curbuf, len); break;13785 case ST_hex : stu__tokval = stu__makeint(strtol(stu__curbuf+2,NULL,16)); stu__tok = ST_number; break;13786 case ST_decimal: stu__tokval = stu__makeint(strtol(stu__curbuf ,NULL,10)); stu__tok = ST_number; break;13787 case ST_float : stu__tokval = stua_float((float) atof(stu__curbuf)) ; stu__tok = ST_number; break;13788 case ST_char : stu__tokval = stu__curbuf[2] == '\\' ? stu__curbuf[3] : stu__curbuf[2];13789 if (stu__curbuf[3] == 't') stu__tokval = '\t';13790 if (stu__curbuf[3] == 'n') stu__tokval = '\n';13791 if (stu__curbuf[3] == 'r') stu__tokval = '\r';13792 stu__tokval = stu__makeint(stu__tokval);13793 stu__tok = ST_number;13794 break;13795 }13796 stu__curbuf += len;13797}1379813799static struct { int stu__tok; char *regex; } stu__lexemes[] =13800{13801 ST_white , "([ \t\n\r]|/\\*(.|\n)*\\*/|//[^\r\n]*([\r\n]|$))+",13802 ST_id , "[_a-zA-Z][_a-zA-Z0-9]*",13803 ST_hex , "0x[0-9a-fA-F]+",13804 ST_decimal, "[0-9]+[0-9]*",13805 ST_float , "[0-9]+\\.?[0-9]*([eE][-+]?[0-9]+)?",13806 ST_float , "\\.[0-9]+([eE][-+]?[0-9]+)?",13807 ST_char , "c'(\\\\.|[^\\'])'",13808 ST_string , "\"(\\\\.|[^\\\"\n\r])*\"",13809 ST_string , "\'(\\\\.|[^\\\'\n\r])*\'",1381013811 #define stua_key4(a,b,c,d) ST_##a, #a, ST_##b, #b, ST_##c, #c, ST_##d, #d,13812 stua_key4(if,then,else,elseif) stua_key4(while,do,for,in)13813 stua_key4(func,var,let,break) stua_key4(nil,true,false,end)13814 stua_key4(return,continue,as,repeat) stua_key4(_frame,catch,catch,catch)1381513816 ST_shl, "<<", ST_and, "&&", ST_eq, "==", ST_ge, ">=",13817 ST_shr, ">>", ST_or , "||", ST_ne, "!=", ST_le, "<=",13818 ST_shru,">>>", ST_into, "=>",13819 T__none, ".",13820};1382113822typedef struct13823{13824 stua_obj *data; // constants being compiled13825 short *code; // code being compiled13826 stua_dict *locals;13827 short *non_local_refs;13828} stu__comp_func;1382913830static stu__comp_func stu__pfunc;13831static stu__comp_func *func_stack = NULL;13832static void stu__push_func_comp(void)13833{13834 stb_arr_push(func_stack, stu__pfunc);13835 stu__pfunc.data = NULL;13836 stu__pfunc.code = NULL;13837 stu__pfunc.locals = stb_idict_new_size(16);13838 stu__pfunc.non_local_refs = NULL;13839 stb_arr_push(stu__pfunc.code, 0); // number of data items13840 stb_arr_push(stu__pfunc.code, 1); // starting execution address13841}1384213843static void stu__pop_func_comp(void)13844{13845 stb_arr_free(stu__pfunc.code);13846 stb_arr_free(stu__pfunc.data);13847 stb_idict_destroy(stu__pfunc.locals);13848 stb_arr_free(stu__pfunc.non_local_refs);13849 stu__pfunc = stb_arr_pop(func_stack);13850}1385113852// if an id is a reference to an outer lexical scope, this13853// function returns the "name" of it, and updates the stack13854// structures to make sure the names are propagated in.13855static int stu__nonlocal_id(stua_obj var_obj)13856{13857 stua_obj dummy, var = var_obj;13858 int i, n = stb_arr_len(func_stack), j,k;13859 if (stb_idict_get_flag(stu__pfunc.locals, var, &dummy)) return 0;13860 for (i=n-1; i > 1; --i) {13861 if (stb_idict_get_flag(func_stack[i].locals, var, &dummy))13862 break;13863 }13864 if (i <= 1) return 0; // stu__compile_global_scope13865 j = i; // need to access variable from j'th frame13866 for (i=0; i < stb_arr_len(stu__pfunc.non_local_refs); ++i)13867 if (stu__pfunc.non_local_refs[i] == j) return j-n;13868 stb_arr_push(stu__pfunc.non_local_refs, j-n);13869 // now make sure all the parents propagate it down13870 for (k=n-1; k > 1; --k) {13871 if (j-k >= 0) return j-n; // comes direct from this parent13872 for(i=0; i < stb_arr_len(func_stack[k].non_local_refs); ++i)13873 if (func_stack[k].non_local_refs[i] == j-k)13874 return j-n;13875 stb_arr_push(func_stack[k].non_local_refs, j-k);13876 }13877 assert (k != 1);1387813879 return j-n;13880}1388113882static int stu__off(void) { return stb_arr_len(stu__pfunc.code); }13883static void stu__cc(int a)13884{13885 assert(a >= -2000 && a < 5000);13886 stb_arr_push(stu__pfunc.code, a);13887}13888static int stu__cc1(int a) { stu__cc(a); return stu__off()-1; }13889static int stu__cc2(int a, int b) { stu__cc(a); stu__cc(b); return stu__off()-2; }13890static int stu__cc3(int a, int b, int c) {13891 if (a == '=') assert(c != 0);13892 stu__cc(a); stu__cc(b); stu__cc(c); return stu__off()-3; }13893static int stu__cc4(int a, int b, int c, int d) { stu__cc(a); stu__cc(b); stu__cc(c); stu__cc(d); return stu__off()-4; }1389413895static int stu__cdv(stua_obj p)13896{13897 int i;13898 assert(p != STUA_NO_VALUE);13899 for (i=0; i < stb_arr_len(stu__pfunc.data); ++i)13900 if (stu__pfunc.data[i] == p)13901 break;13902 if (i == stb_arr_len(stu__pfunc.data))13903 stb_arr_push(stu__pfunc.data, p);13904 return ~i;13905}1390613907static int stu__cdt(void)13908{13909 int z = stu__cdv(stu__tokval);13910 stu__nexttoken();13911 return z;13912}1391313914static int stu__seq(int a, int b)13915{13916 return !a ? b : !b ? a : stu__cc3(STU__seq, a,b);13917}1391813919static char stu__comp_err_str[1024];13920static int stu__comp_err_line;13921static int stu__err(char *str, ...)13922{13923 va_list v;13924 char *s = stu__bufstart;13925 stu__comp_err_line = 1;13926 while (s < stu__curbuf) {13927 if (s[0] == '\n' || s[0] == '\r') {13928 if (s[0]+s[1] == '\n' + '\r') ++s;13929 ++stu__comp_err_line;13930 }13931 ++s;13932 }13933 va_start(v, str);13934 vsprintf(stu__comp_err_str, str, v);13935 va_end(v);13936 return 0;13937}1393813939static int stu__accept(int p)13940{13941 if (stu__tok != p) return 0;13942 stu__nexttoken();13943 return 1;13944}1394513946static int stu__demand(int p)13947{13948 if (stu__accept(p)) return 1;13949 return stu__err("Didn't find expected stu__tok");13950}1395113952static int stu__demandv(int p, stua_obj *val)13953{13954 if (stu__tok == p || p==0) {13955 *val = stu__tokval;13956 stu__nexttoken();13957 return 1;13958 } else13959 return 0;13960}1396113962static int stu__expr(int p);13963int stu__nexpr(int p) { stu__nexttoken(); return stu__expr(p); }13964static int stu__statements(int once, int as);1396513966static int stu__parse_if(void) // parse both ST_if and ST_elseif13967{13968 int b,c,a;13969 a = stu__nexpr(1); if (!a) return 0;13970 if (!stu__demand(ST_then)) return stu__err("expecting THEN");13971 b = stu__statements(0,0); if (!b) return 0;13972 if (b == 1) b = -1;1397313974 if (stu__tok == ST_elseif) {13975 return stu__parse_if();13976 } else if (stu__accept(ST_else)) {13977 c = stu__statements(0,0); if (!c) return 0;13978 if (!stu__demand(ST_end)) return stu__err("expecting END after else clause");13979 return stu__cc4(ST_else, a, b, c);13980 } else {13981 if (!stu__demand(ST_end)) return stu__err("expecting END in if statement");13982 return stu__cc3(ST_if, a, b);13983 }13984}1398513986int stu__varinit(int z, int in_globals)13987{13988 int a,b;13989 stu__nexttoken();13990 while (stu__demandv(ST_id, &b)) {13991 if (!stb_idict_add(stu__pfunc.locals, b, 1))13992 if (!in_globals) return stu__err("Redefined variable %s.", stu__pw(b)->ptr);13993 if (stu__accept('=')) {13994 a = stu__expr(1); if (!a) return 0;13995 } else13996 a = stu__cdv(stua_nil);13997 z = stu__seq(z, stu__cc3(ST_var, stu__cdv(b), a));13998 if (!stu__accept(',')) break;13999 }14000 return z;14001}1400214003static int stu__compile_unary(int z, int outparm, int require_inparm)14004{14005 int op = stu__tok, a, b;14006 stu__nexttoken();14007 if (outparm) {14008 if (require_inparm || (stu__tok && stu__tok != ST_end && stu__tok != ST_else && stu__tok != ST_elseif && stu__tok !=';')) {14009 a = stu__expr(1); if (!a) return 0;14010 } else14011 a = stu__cdv(stua_nil);14012 b = stu__cc2(op, a);14013 } else14014 b = stu__cc1(op);14015 return stu__seq(z,b);14016}1401714018static int stu__assign(void)14019{14020 int z;14021 stu__accept(ST_let);14022 z = stu__expr(1); if (!z) return 0;14023 if (stu__accept('=')) {14024 int y,p = (z >= 0 ? stu__pfunc.code[z] : 0);14025 if (z < 0 || (p != ST_id && p != '[')) return stu__err("Invalid lvalue in assignment");14026 y = stu__assign(); if (!y) return 0;14027 z = stu__cc3('=', z, y);14028 }14029 return z;14030}1403114032static int stu__statements(int once, int stop_while)14033{14034 int a,b, c, z=0;14035 for(;;) {14036 switch (stu__tok) {14037 case ST_if : a = stu__parse_if(); if (!a) return 0;14038 z = stu__seq(z, a);14039 break;14040 case ST_while : if (stop_while) return (z ? z:1);14041 a = stu__nexpr(1); if (!a) return 0;14042 if (stu__accept(ST_as)) c = stu__statements(0,0); else c = 0;14043 if (!stu__demand(ST_do)) return stu__err("expecting DO");14044 b = stu__statements(0,0); if (!b) return 0;14045 if (!stu__demand(ST_end)) return stu__err("expecting END");14046 if (b == 1) b = -1;14047 z = stu__seq(z, stu__cc4(ST_while, a, b, c));14048 break;14049 case ST_repeat : stu__nexttoken();14050 c = stu__statements(0,1); if (!c) return 0;14051 if (!stu__demand(ST_while)) return stu__err("expecting WHILE");14052 a = stu__expr(1); if (!a) return 0;14053 if (!stu__demand(ST_do)) return stu__err("expecting DO");14054 b = stu__statements(0,0); if (!b) return 0;14055 if (!stu__demand(ST_end)) return stu__err("expecting END");14056 if (b == 1) b = -1;14057 z = stu__seq(z, stu__cc4(ST_as, a, b, c));14058 break;14059 case ST_catch : a = stu__nexpr(1); if (!a) return 0;14060 z = stu__seq(z, stu__cc2(ST_catch, a));14061 break;14062 case ST_var : z = stu__varinit(z,0); break;14063 case ST_return : z = stu__compile_unary(z,1,1); break;14064 case ST_continue:z = stu__compile_unary(z,0,0); break;14065 case ST_break : z = stu__compile_unary(z,1,0); break;14066 case ST_into : if (z == 0 && !once) return stu__err("=> cannot be first statement in block");14067 a = stu__nexpr(99);14068 b = (a >= 0? stu__pfunc.code[a] : 0);14069 if (a < 0 || (b != ST_id && b != '[')) return stu__err("Invalid lvalue on right side of =>");14070 z = stu__cc3('=', a, z);14071 break;14072 default : if (stu__end[stu__tok]) return once ? 0 : (z ? z:1);14073 a = stu__assign(); if (!a) return 0;14074 stu__accept(';');14075 if (stu__tok && !stu__end[stu__tok]) {14076 if (a < 0)14077 return stu__err("Constant has no effect");14078 if (stu__pfunc.code[a] != '(' && stu__pfunc.code[a] != '=')14079 return stu__err("Expression has no effect");14080 }14081 z = stu__seq(z, a);14082 break;14083 }14084 if (!z) return 0;14085 stu__accept(';');14086 if (once && stu__tok != ST_into) return z;14087 }14088}1408914090static int stu__postexpr(int z, int p);14091static int stu__dictdef(int end, int *count)14092{14093 int z,n=0,i,flags=0;14094 short *dict=NULL;14095 stu__nexttoken();14096 while (stu__tok != end) {14097 if (stu__tok == ST_id) {14098 stua_obj id = stu__tokval;14099 stu__nexttoken();14100 if (stu__tok == '=') {14101 flags |= 1;14102 stb_arr_push(dict, stu__cdv(id));14103 z = stu__nexpr(1); if (!z) return 0;14104 } else {14105 z = stu__cc2(ST_id, stu__cdv(id));14106 z = stu__postexpr(z,1); if (!z) return 0;14107 flags |= 2;14108 stb_arr_push(dict, stu__cdv(stu__makeint(n++)));14109 }14110 } else {14111 z = stu__expr(1); if (!z) return 0;14112 flags |= 2;14113 stb_arr_push(dict, stu__cdv(stu__makeint(n++)));14114 }14115 if (end != ')' && flags == 3) { z=stu__err("can't mix initialized and uninitialized defs"); goto done;}14116 stb_arr_push(dict, z);14117 if (!stu__accept(',')) break;14118 }14119 if (!stu__demand(end))14120 return stu__err(end == ')' ? "Expecting ) at end of function call"14121 : "Expecting } at end of dictionary definition");14122 z = stu__cc2('{', stb_arr_len(dict)/2);14123 for (i=0; i < stb_arr_len(dict); ++i)14124 stu__cc(dict[i]);14125 if (count) *count = n;14126done:14127 stb_arr_free(dict);14128 return z;14129}1413014131static int stu__comp_id(void)14132{14133 int z,d;14134 d = stu__nonlocal_id(stu__tokval);14135 if (d == 0)14136 return z = stu__cc2(ST_id, stu__cdt());14137 // access a non-local frame by naming it with the appropriate int14138 assert(d < 0);14139 z = stu__cdv(d); // relative frame # is the 'variable' in our local frame14140 z = stu__cc2(ST_id, z); // now access that dictionary14141 return stu__cc3('[', z, stu__cdt()); // now access the variable from that dir14142}1414314144static stua_obj stu__funcdef(stua_obj *id, stua_obj *func);14145static int stu__expr(int p)14146{14147 int z;14148 // unary14149 switch (stu__tok) {14150 case ST_number: z = stu__cdt(); break;14151 case ST_string: z = stu__cdt(); break; // @TODO - string concatenation like C14152 case ST_id : z = stu__comp_id(); break;14153 case ST__frame: z = stu__cc1(ST__frame); stu__nexttoken(); break;14154 case ST_func : z = stu__funcdef(NULL,NULL); break;14155 case ST_if : z = stu__parse_if(); break;14156 case ST_nil : z = stu__cdv(stua_nil); stu__nexttoken(); break;14157 case ST_true : z = stu__cdv(stua_true); stu__nexttoken(); break;14158 case ST_false : z = stu__cdv(stua_false); stu__nexttoken(); break;14159 case '-' : z = stu__nexpr(99); if (z) z=stu__cc2(STU__negate,z); else return z; break;14160 case '!' : z = stu__nexpr(99); if (z) z=stu__cc2('!',z); else return z; break;14161 case '~' : z = stu__nexpr(99); if (z) z=stu__cc2('~',z); else return z; break;14162 case '{' : z = stu__dictdef('}', NULL); break;14163 default : return stu__err("Unexpected token");14164 case '(' : stu__nexttoken(); z = stu__statements(0,0); if (!stu__demand(')')) return stu__err("Expecting )");14165 }14166 return stu__postexpr(z,p);14167}1416814169static int stu__postexpr(int z, int p)14170{14171 int q;14172 // postfix14173 while (stu__tok == '(' || stu__tok == '[' || stu__tok == '.') {14174 if (stu__accept('.')) {14175 // MUST be followed by a plain identifier! use [] for other stuff14176 if (stu__tok != ST_id) return stu__err("Must follow . with plain name; try [] instead");14177 z = stu__cc3('[', z, stu__cdv(stu__tokval));14178 stu__nexttoken();14179 } else if (stu__accept('[')) {14180 while (stu__tok != ']') {14181 int r = stu__expr(1); if (!r) return 0;14182 z = stu__cc3('[', z, r);14183 if (!stu__accept(',')) break;14184 }14185 if (!stu__demand(']')) return stu__err("Expecting ]");14186 } else {14187 int n, p = stu__dictdef(')', &n); if (!p) return 0;14188 #if 0 // this is incorrect!14189 if (z > 0 && stu__pfunc.code[z] == ST_id) {14190 stua_obj q = stu__get(stu__globaldict, stu__pfunc.data[-stu__pfunc.code[z+1]-1], stua_nil);14191 if (stu__checkt(STU___function, q))14192 if ((stu__pf(q))->num_param != n)14193 return stu__err("Incorrect number of parameters");14194 }14195 #endif14196 z = stu__cc3('(', z, p);14197 }14198 }14199 // binop - this implementation taken from lcc14200 for (q=stu__prec[stu__tok]; q >= p; --q) {14201 while (stu__prec[stu__tok] == q) {14202 int o = stu__tok, y = stu__nexpr(p+1); if (!y) return 0;14203 z = stu__cc3(o,z,y);14204 }14205 }14206 return z;14207}1420814209static stua_obj stu__finish_func(stua_obj *param, int start)14210{14211 int n, size;14212 stu__func *f = (stu__func *) malloc(sizeof(*f));14213 f->closure_source = 0;14214 f->num_param = stb_arr_len(param);14215 f->param = (int *) stb_copy(param, f->num_param * sizeof(*f->param));14216 size = stb_arr_storage(stu__pfunc.code) + stb_arr_storage(stu__pfunc.data) + sizeof(*f) + 8;14217 f->f.store = malloc(stb_arr_storage(stu__pfunc.code) + stb_arr_storage(stu__pfunc.data));14218 f->code = (short *) ((char *) f->f.store + stb_arr_storage(stu__pfunc.data));14219 memcpy(f->code, stu__pfunc.code, stb_arr_storage(stu__pfunc.code));14220 f->code[1] = start;14221 f->code[0] = stb_arr_len(stu__pfunc.data);14222 for (n=0; n < f->code[0]; ++n)14223 ((stua_obj *) f->code)[-1-n] = stu__pfunc.data[n];14224 return stu__makeobj(STU___function, f, size, 0);14225}1422614227static int stu__funcdef(stua_obj *id, stua_obj *result)14228{14229 int n,z=0,i,q;14230 stua_obj *param = NULL;14231 short *nonlocal;14232 stua_obj v,f=stua_nil;14233 assert(stu__tok == ST_func);14234 stu__nexttoken();14235 if (id) {14236 if (!stu__demandv(ST_id, id)) return stu__err("Expecting function name");14237 } else14238 stu__accept(ST_id);14239 if (!stu__demand('(')) return stu__err("Expecting ( for function parameter");14240 stu__push_func_comp();14241 while (stu__tok != ')') {14242 if (!stu__demandv(ST_id, &v)) { z=stu__err("Expecting parameter name"); goto done; }14243 stb_idict_add(stu__pfunc.locals, v, 1);14244 if (stu__tok == '=') {14245 n = stu__nexpr(1); if (!n) { z=0; goto done; }14246 z = stu__seq(z, stu__cc3(STU__defaultparm, stu__cdv(v), n));14247 } else14248 stb_arr_push(param, v);14249 if (!stu__accept(',')) break;14250 }14251 if (!stu__demand(')')) { z=stu__err("Expecting ) at end of parameter list"); goto done; }14252 n = stu__statements(0,0); if (!n) { z=0; goto done; }14253 if (!stu__demand(ST_end)) { z=stu__err("Expecting END at end of function"); goto done; }14254 if (n == 1) n = 0;14255 n = stu__seq(z,n);14256 f = stu__finish_func(param, n);14257 if (result) { *result = f; z=1; stu__pop_func_comp(); }14258 else {14259 nonlocal = stu__pfunc.non_local_refs;14260 stu__pfunc.non_local_refs = NULL;14261 stu__pop_func_comp();14262 z = stu__cdv(f);14263 if (nonlocal) { // build a closure with references to the needed frames14264 short *initcode = NULL;14265 for (i=0; i < stb_arr_len(nonlocal); ++i) {14266 int k = nonlocal[i], p;14267 stb_arr_push(initcode, stu__cdv(k));14268 if (k == -1) p = stu__cc1(ST__frame);14269 else { p = stu__cdv(stu__makeint(k+1)); p = stu__cc2(ST_id, p); }14270 stb_arr_push(initcode, p);14271 }14272 q = stu__cc2('{', stb_arr_len(nonlocal));14273 for (i=0; i < stb_arr_len(initcode); ++i)14274 stu__cc(initcode[i]);14275 z = stu__cc3('+', z, q);14276 stb_arr_free(initcode);14277 }14278 stb_arr_free(nonlocal);14279 }14280done:14281 stb_arr_free(param);14282 if (!z) stu__pop_func_comp();14283 return z;14284}1428514286static int stu__compile_global_scope(void)14287{14288 stua_obj o;14289 int z=0;1429014291 stu__push_func_comp();14292 while (stu__tok != 0) {14293 if (stu__tok == ST_func) {14294 stua_obj id, f;14295 if (!stu__funcdef(&id,&f))14296 goto error;14297 stu__set(stu__globaldict, id, f);14298 } else if (stu__tok == ST_var) {14299 z = stu__varinit(z,1); if (!z) goto error;14300 } else {14301 int y = stu__statements(1,0); if (!y) goto error;14302 z = stu__seq(z,y);14303 }14304 stu__accept(';');14305 }14306 o = stu__finish_func(NULL, z);14307 stu__pop_func_comp();1430814309 o = stu__funceval(o, stua_globals); // initialize stu__globaldict14310 if (stu__flow == FLOW_error)14311 printf("Error: %s\n", ((stu__wrapper *) stu__ptr(stu__flow_val))->ptr);14312 return 1;14313error:14314 stu__pop_func_comp();14315 return 0;14316}1431714318stua_obj stu__myprint(stua_dict *context)14319{14320 stua_obj x = stu__get(context, stua_string("x"), stua_nil);14321 if ((x & 1) == stu__float_tag) printf("%f", stu__getfloat(x));14322 else if (stu__tag(x) == stu__int_tag) printf("%d", stu__int(x));14323 else {14324 stu__wrapper *s = stu__pw(x);14325 if (s->type == STU___string || s->type == STU___error)14326 printf("%s", s->ptr);14327 else if (s->type == STU___dict) printf("{{dictionary}}");14328 else if (s->type == STU___function) printf("[[function]]");14329 else14330 printf("[[ERROR:%s]]", s->ptr);14331 }14332 return x;14333}1433414335void stua_init(void)14336{14337 if (!stu__globaldict) {14338 int i;14339 stua_obj s;14340 stu__func *f;1434114342 stu__prec[ST_and] = stu__prec[ST_or] = 1;14343 stu__prec[ST_eq ] = stu__prec[ST_ne] = stu__prec[ST_le] =14344 stu__prec[ST_ge] = stu__prec['>' ] = stu__prec['<'] = 2;14345 stu__prec[':'] = 3;14346 stu__prec['&'] = stu__prec['|'] = stu__prec['^'] = 4;14347 stu__prec['+'] = stu__prec['-'] = 5;14348 stu__prec['*'] = stu__prec['/'] = stu__prec['%'] =14349 stu__prec[ST_shl]= stu__prec[ST_shr]= stu__prec[ST_shru]= 6;1435014351 stu__end[')'] = stu__end[ST_end] = stu__end[ST_else] = 1;14352 stu__end[ST_do] = stu__end[ST_elseif] = 1;1435314354 stu__float_init();14355 stu__lex_matcher = stb_lex_matcher();14356 for (i=0; i < sizeof(stu__lexemes)/sizeof(stu__lexemes[0]); ++i)14357 stb_lex_item(stu__lex_matcher, stu__lexemes[i].regex, stu__lexemes[i].stu__tok);1435814359 stu__globaldict = stb_idict_new_size(64);14360 stua_globals = stu__makeobj(STU___dict, stu__globaldict, 0,0);14361 stu__strings = stb_sdict_new(0);1436214363 stu__curbuf = stu__bufstart = "func _print(x) end\n"14364 "func print()\n var x=0 while _frame[x] != nil as x=x+1 do _print(_frame[x]) end end\n";14365 stu__nexttoken();14366 if (!stu__compile_global_scope())14367 printf("Compile error in line %d: %s\n", stu__comp_err_line, stu__comp_err_str);1436814369 s = stu__get(stu__globaldict, stua_string("_print"), stua_nil);14370 if (stu__tag(s) == stu__ptr_tag && stu__ptr(s)->type == STU___function) {14371 f = stu__pf(s);14372 free(f->f.store);14373 f->closure_source = 4;14374 f->f.func = stu__myprint;14375 f->code = NULL;14376 }14377 }14378}1437914380void stua_uninit(void)14381{14382 if (stu__globaldict) {14383 stb_idict_remove_all(stu__globaldict);14384 stb_arr_setlen(stu__gc_root_stack, 0);14385 stua_gc(1);14386 stb_idict_destroy(stu__globaldict);14387 stb_sdict_delete(stu__strings);14388 stb_matcher_free(stu__lex_matcher);14389 stb_arr_free(stu__gc_ptrlist);14390 stb_arr_free(func_stack);14391 stb_arr_free(stu__gc_root_stack);14392 stu__globaldict = NULL;14393 }14394}1439514396void stua_run_script(char *s)14397{14398 stua_init();1439914400 stu__curbuf = stu__bufstart = s;14401 stu__nexttoken();1440214403 stu__flow = FLOW_normal;1440414405 if (!stu__compile_global_scope())14406 printf("Compile error in line %d: %s\n", stu__comp_err_line, stu__comp_err_str);14407 stua_gc(1);14408}14409#endif // STB_DEFINE14410#endif // STB_STUA1441113062#undef STB_EXTERN13063#endif // STB_INCLUDE_STB_H1306413065/*
40 unmodified lines
added:tests/pbm/basi0g16.pgmnot shown

Its text is not shown, added.

added:tests/pbm/basi2c16.ppmnot shown

Its text is not shown, added.

added:tests/pbm/cdfn2c08.ppmnot shown

Its text is not shown, added.

added:tests/pbm/cdun2c08.ppmnot shown

Its text is not shown, added.

added:tests/pbm/comment.pgmnot shown

Its text is not shown, added.

added:tests/pbm/ctfn0g04.pgmnot shown

Its text is not shown, added.

modified:tests/ossfuzz.shnot shown

Its text is not shown, modified.

modified:.travis.yml+3 added
1language: C2arch:3 - AMD644 - ppc64le5install: true6script:7 - cd tests8 - make all
modified:README.md+3 added-2 removed
18 unmodified lines
19<a name="stb_libs"></a>2021library | lastest version | category | LoC | description22--------------------- | ---- | -------- | --- | --------------------------------23<<<<<<< HEAD23**[stb_vorbis.c](stb_vorbis.c)** | 1.20 | audio | 5563 | decode ogg vorbis files from file/memory to float/16-bit signed output24**[stb_vorbis.c](stb_vorbis.c)** | 1.21 | audio | 5569 | decode ogg vorbis files from file/memory to float/16-bit signed output25**[stb_image.h](stb_image.h)** | 2.26 | graphics | 7762 | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC, PNM26**[stb_truetype.h](stb_truetype.h)** | 1.24 | graphics | 5011 | parse, decode, and rasterize characters from truetype fonts27**[stb_image_write.h](stb_image_write.h)** | 1.15 | graphics | 1690 | image writing to disk: PNG, TGA, BMP, JPG, HDR28**[stb_image_resize.h](stb_image_resize.h)** | 0.96 | graphics | 2631 | resize images larger/smaller with good quality
14 unmodified lines
43**[stb_leakcheck.h](stb_leakcheck.h)** | 0.6 | misc | 194 | quick-and-dirty malloc/free leak-checking44**[stb_include.h](stb_include.h)** | 0.02 | misc | 295 | implement recursive #include support, particularly for GLSL4546Total libraries: 2146Total lines of C code: 5652447Total lines of C code: 56530484950FAQ51---
132 unmodified lines
modified:stb_ds.h+2 added-2 removed
102 unmodified lines
103 Inserts the item b into the middle of array a, into a[p],104 moving the rest of the array over. Returns b.105106 arrinsn:107 void arrins(T* a, int p, int n);107 void arrinsn(T* a, int p, int n);108 Inserts n uninitialized items into array a starting at a[p],109 moving the rest of the array over.110111 arraddnptr:
10 unmodified lines
122 void arrdel(T* a, int p);123 Deletes the element at a[p], moving the rest of the array over.124125 arrdeln:126 void arrdel(T* a, int p, int n);126 void arrdeln(T* a, int p, int n);127 Deletes n elements starting at a[p], moving the rest of the array over.128129 arrdelswap:130 void arrdelswap(T* a, int p);
1765 unmodified lines
modified:stb_image_write.h+12 added-11 removed
176 unmodified lines
177STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);178STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);179STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);180181#ifdef STBI_WINDOWS_UTF8181#ifdef STBIW_WINDOWS_UTF8182STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);183#endif184#endif185
98 unmodified lines
284{285 fwrite(data,1,size,(FILE*) context);286}287288#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)288#if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8)289#ifdef __cplusplus290#define STBIW_EXTERN extern "C"291#else292#define STBIW_EXTERN extern
2 unmodified lines
295STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);296297STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)298{299 return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);299 return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);300}301#endif302303static FILE *stbiw__fopen(char const *filename, char const *mode)304{305 FILE *f;306#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)306#if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8)307 wchar_t wMode[64];308 wchar_t wFilename[1024];309 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename)))309 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename)))310 return 0;311312 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode)))312 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode)))313 return 0;314315#if defined(_MSC_VER) && _MSC_VER >= 1400316 if (0 != _wfopen_s(&f, wFilename, wMode))317 f = 0;316 if (0 != _wfopen_s(&f, wFilename, wMode))317 f = 0;318#else319 f = _wfopen(wFilename, wMode);320#endif321
299 unmodified lines
621// by Baldur Karlsson622623#define stbiw__max(a, b) ((a) > (b) ? (a) : (b))624625#ifndef STBI_WRITE_NO_STDIO626627static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)628{629 int exponent;630 float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));
126 unmodified lines
757 char buffer[128];758 char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";759 s->func(s->context, header, sizeof(header)-1);760759#ifdef __STDC_WANT_SECURE_LIB__761#ifdef __STDC_LIB_EXT1__762 len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);763#else764 len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);765#endif
12 unmodified lines
778 stbi__start_write_callbacks(&s, func, context);779 return stbi_write_hdr_core(&s, x, y, comp, (float *) data);780}781780#ifndef STBI_WRITE_NO_STDIO782STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)783{784 stbi__write_context s = { 0 };785 if (stbi__start_write_file(&s,filename)) {
849 unmodified lines
1635 0.96 (2015-01-17)1636 add HDR output1637 fix monochrome BMP1638 0.95 (2014-08-17)1638 add monochrome TGA output1639 add monochrome TGA output1640 0.94 (2014-05-31)1641 rename private functions to avoid conflicts with stb_image.h1642 0.93 (2014-05-27)1643 warning fixes
48 unmodified lines
modified:stb_image.h+80 added-26 removed
109 unmodified lines
110 Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus111 Josh Tobin Matthew Gregan github:poppolopoppo112 Julian Raschke Gregory Mullen Christian Floisand github:darealshinji113 Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007114 Brad Weinberger Matvey Cherevko [reserved]114 Brad Weinberger Matvey Cherevko github:mosra115 Luca Sas Alexander Veselov Zack Middleton [reserved]116 Ryan C. Gordon [reserved] [reserved]117 DO NOT ADD YOUR NAME HERE118
190 unmodified lines
309// ===========================================================================310//311// iPhone PNG support:312//313// By default we convert iphone-formatted PNGs back to RGB, even though314// they are internally encoded differently. You can disable this conversion315// by calling stbi_convert_iphone_png_to_rgb(0), in which case316// you will always just get the native iphone "format" through (which317// is BGR stored in RGB).313// We optionally support converting iPhone-formatted PNGs (which store314// premultiplied BGRA) back to RGB, even though they're internally encoded315// differently. To enable this conversion, call316// stbi_convert_iphone_png_to_rgb(1).317//318// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per319// pixel to remove any premultiplied alpha *only* if the image file explicitly320// says there's premultiplied data (currently only happens in iPhone images,
195 unmodified lines
516517// as above, but only applies to images loaded on the thread that calls the function518// this function is only available if your compiler supports thread-local variables;519// calling it will fail to link if your compiler doesn't520STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply);521STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert);522STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);523524// ZLIB client - used by PNG, available for other purposes525
3295 unmodified lines
3821 decode_n = 1;3822 else3823 decode_n = z->s->img_n;38243825 // nothing to do if no components requested; check this now to avoid3826 // accessing uninitialized coutput[0] later3827 if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; }38283829 // resample and color-convert3830 {3831 int k;3832 unsigned int i,j;
1094 unmodified lines
49274928 return 1;4929}49304926static int stbi__unpremultiply_on_load = 0;4927static int stbi__de_iphone_flag = 0;4931static int stbi__unpremultiply_on_load_global = 0;4932static int stbi__de_iphone_flag_global = 0;49334934STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)4935{4931 stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;4936 stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply;4937}49384939STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)4940{4936 stbi__de_iphone_flag = flag_true_if_should_convert;4941 stbi__de_iphone_flag_global = flag_true_if_should_convert;4942}49434944#ifndef STBI_THREAD_LOCAL4945#define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global4946#define stbi__de_iphone_flag stbi__de_iphone_flag_global4947#else4948static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set;4949static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set;49504951STBIDEF void stbi__unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply)4952{4953 stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply;4954 stbi__unpremultiply_on_load_set = 1;4955}49564957STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert)4958{4959 stbi__de_iphone_flag_local = flag_true_if_should_convert;4960 stbi__de_iphone_flag_set = 1;4961}49624963#define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \4964 ? stbi__unpremultiply_on_load_local \4965 : stbi__unpremultiply_on_load_global)4966#define stbi__de_iphone_flag (stbi__de_iphone_flag_set \4967 ? stbi__de_iphone_flag_local \4968 : stbi__de_iphone_flag_global)4969#endif // STBI_THREAD_LOCAL49704971static void stbi__de_iphone(stbi__png *z)4972{4973 stbi__context *s = z->s;4974 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
372 unmodified lines
5347 unsigned int mr,mg,mb,ma, all_a;5348 int extra_read;5349} stbi__bmp_data;53505351static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress)5352{5353 // BI_BITFIELDS specifies masks explicitly, don't override5354 if (compress == 3)5355 return 1;53565357 if (compress == 0) {5358 if (info->bpp == 16) {5359 info->mr = 31u << 10;5360 info->mg = 31u << 5;5361 info->mb = 31u << 0;5362 } else if (info->bpp == 32) {5363 info->mr = 0xffu << 16;5364 info->mg = 0xffu << 8;5365 info->mb = 0xffu << 0;5366 info->ma = 0xffu << 24;5367 info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 05368 } else {5369 // otherwise, use defaults, which is all-05370 info->mr = info->mg = info->mb = info->ma = 0;5371 }5372 return 1;5373 }5374 return 0; // error5375}53765377static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)5378{5379 int hsz;5380 if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP");
19 unmodified lines
5400 info->bpp = stbi__get16le(s);5401 if (hsz != 12) {5402 int compress = stbi__get32le(s);5403 if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");5404 if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG", "BMP type not supported: unsupported compression"); // this includes PNG/JPEG modes5405 if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP", "bad BMP"); // bitfields requires 16 or 32 bits/pixel5406 stbi__get32le(s); // discard sizeof5407 stbi__get32le(s); // discard hres5408 stbi__get32le(s); // discard vres5409 stbi__get32le(s); // discard colorsused
6 unmodified lines
5416 stbi__get32le(s);5417 }5418 if (info->bpp == 16 || info->bpp == 32) {5419 if (compress == 0) {5360 if (info->bpp == 32) {5361 info->mr = 0xffu << 16;5362 info->mg = 0xffu << 8;5363 info->mb = 0xffu << 0;5364 info->ma = 0xffu << 24;5365 info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 05366 } else {5367 info->mr = 31u << 10;5368 info->mg = 31u << 5;5369 info->mb = 31u << 0;5370 }5420 stbi__bmp_set_mask_defaults(info, compress);5421 } else if (compress == 3) {5422 info->mr = stbi__get32le(s);5423 info->mg = stbi__get32le(s);5424 info->mb = stbi__get32le(s);
6 unmodified lines
5431 } else5432 return stbi__errpuc("bad BMP", "bad BMP");5433 }5434 } else {5435 // V4/V5 header5436 int i;5437 if (hsz != 108 && hsz != 124)5438 return stbi__errpuc("bad BMP", "bad BMP");5439 info->mr = stbi__get32le(s);5440 info->mg = stbi__get32le(s);5441 info->mb = stbi__get32le(s);5442 info->ma = stbi__get32le(s);5443 if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs5444 stbi__bmp_set_mask_defaults(info, compress);5445 stbi__get32le(s); // discard color space5446 for (i=0; i < 12; ++i)5447 stbi__get32le(s); // discard color space parameters5448 if (hsz == 124) {
1465 unmodified lines
6914 out_size = layers * stride;6915 }69166917 if (delays) {6865 *delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );6866 if (!*delays)6918 int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );6919 if (!new_delays)6920 return stbi__load_gif_main_outofmem(&g, out, delays);6921 *delays = new_delays;6922 delays_size = layers * sizeof(int);6923 }6924 } else {6925 out = (stbi_uc*)stbi__malloc( layers * stride );
327 unmodified lines
7253 stbi__bmp_data info;72547255 info.all_a = 255;7256 p = stbi__bmp_parse_header(s, &info);7203 stbi__rewind( s );7204 if (p == NULL)7257 if (p == NULL) {7258 stbi__rewind( s );7259 return 0;7260 }7261 if (x) *x = s->img_x;7262 if (y) *y = s->img_y;7263 if (comp) {7264 if (info.bpp == 24 && info.ma == 0xff000000)
263 unmodified lines
7528 if (stbi__pnm_info(s, NULL, NULL, NULL) == 16)7529 return 1;7530 return 0;7531}74777532#endif75337534static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)7535{
355 unmodified lines
modified:stb_textedit.h+52 added-27 removed
1// stb_textedit.h - v1.13 - public domain - Sean Barrett1// stb_textedit.h - v1.14 - public domain - Sean Barrett2// Development of this library was sponsored by RAD Game Tools3//4// This C header file implements the guts of a multi-line text-editing5// widget; you implement display, word-wrapping, and low-level string
22 unmodified lines
28//29//30// VERSION HISTORY31//32// 1.14 ( ) page up/down, various fixes33// 1.13 (2019-02-07) fix bug in undo size management34// 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash35// 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield36// 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual
15 unmodified lines
52//53// Ulf Winklemann: move-by-word in 1.154// Fabian Giesen: secondary key inputs in 1.555// Martins Mozeiko: STB_TEXTEDIT_memmove in 1.656// Louis Schnellbach: page up/down in 1.1457//58// Bugfixes:59// Scott Graham60// Daniel Keller
82 unmodified lines
143// STB_TEXTEDIT_K_LEFT keyboard input to move cursor left144// STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right145// STB_TEXTEDIT_K_UP keyboard input to move cursor up146// STB_TEXTEDIT_K_DOWN keyboard input to move cursor down147// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page148// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page149// STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME150// STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END151// STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME152// STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END
14 unmodified lines
167// STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line168// STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text169// STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text170//167// Todo:168// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page169// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page170//171// Keyboard input must be encoded as a single integer value; e.g. a character code172// and some bitflags that represent shift states. to simplify the interface, SHIFT must173// be a bitflag, so we can test the shifted state of cursor movements to allow selection,174// i.e. (STB_TEXTEDIT_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow.
155 unmodified lines
330 unsigned char insert_mode;331 // each textfield keeps its own insert mode state. to keep an app-wide332 // insert mode, copy this value in/out of the app state333334 int row_count_per_page;335 // page size in number of row.336 // this value MUST be set to >0 for pageup or pagedown in multilines documents.337338 /////////////////////339 //340 // private data341 //
369 unmodified lines
711 state->cursor += len;712 state->has_preferred_x = 0;713 return 1;714 }711 // remove the undo since we didn't actually insert the characters712 if (state->undostate.undo_point)713 --state->undostate.undo_point;715 // note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details)716 return 0;717}718719#ifndef STB_TEXTEDIT_KEYTYPE
130 unmodified lines
850 state->has_preferred_x = 0;851 break;852853 case STB_TEXTEDIT_K_DOWN:852 case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: {854 case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT:855 case STB_TEXTEDIT_K_PGDOWN:856 case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: {857 StbFindState find;858 StbTexteditRow row;855 int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;859 int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;860 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN;861 int row_count = is_page ? state->row_count_per_page : 1;862857 if (state->single_line) {863 if (!is_page && state->single_line) {864 // on windows, up&down in single-line behave like left&right865 key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);866 goto retry;867 }868869 if (sel)870 stb_textedit_prep_selection_at_cursor(state);871 else if (STB_TEXT_HAS_SELECTION(state))866 stb_textedit_move_to_last(str,state);872 stb_textedit_move_to_last(str, state);873874 // compute current position of cursor point875 stb_textedit_clamp(str, state);876 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);877872 // now find character position down a row873 if (find.length) {874 float goal_x = state->has_preferred_x ? state->preferred_x : find.x;878 for (j = 0; j < row_count; ++j) {879 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;875 float x;880 int start = find.first_char + find.length;881882 if (find.length == 0)883 break;884885 // now find character position down a row886 state->cursor = start;887 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);888 x = row.x0;889 for (i=0; i < row.num_chars; ++i) {
13 unmodified lines
903 state->preferred_x = goal_x;904905 if (sel)906 state->select_end = state->cursor;907908 // go to next line909 find.first_char = find.first_char + find.length;910 find.length = row.num_chars;911 }912 break;913 }914915 case STB_TEXTEDIT_K_UP:903 case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: {916 case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT:917 case STB_TEXTEDIT_K_PGUP:918 case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: {919 StbFindState find;920 StbTexteditRow row;906 int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;921 int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;922 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP;923 int row_count = is_page ? state->row_count_per_page : 1;924908 if (state->single_line) {925 if (!is_page && state->single_line) {926 // on windows, up&down become left&right927 key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);928 goto retry;929 }
6 unmodified lines
936 // compute current position of cursor point937 stb_textedit_clamp(str, state);938 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);939940 for (j = 0; j < row_count; ++j) {941 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;942923 // can only go up if there's a previous row924 if (find.prev_first != find.first_char) {943 // can only go up if there's a previous row944 if (find.prev_first == find.first_char)945 break;946947 // now find character position up a row926 float goal_x = state->has_preferred_x ? state->preferred_x : find.x;927 float x;948 state->cursor = find.prev_first;949 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);950 x = row.x0;951 for (i=0; i < row.num_chars; ++i) {
13 unmodified lines
965 state->preferred_x = goal_x;966967 if (sel)968 state->select_end = state->cursor;969970 // go to previous line971 // (we need to scan previous line the hard way. maybe we could expose this as a new API function?)972 prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0;973 while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE)974 --prev_scan;975 find.first_char = find.prev_first;976 find.prev_first = prev_scan;977 }978 break;979 }980
115 unmodified lines
1096 state->select_end = state->cursor;1097 state->has_preferred_x = 0;1098 break;1099 }10721073// @TODO:1074// STB_TEXTEDIT_K_PGUP - move cursor up a page1075// STB_TEXTEDIT_K_PGDOWN - move cursor down a page1100 }1101}11021103/////////////////////////////////////////////////////////////////////////////
256 unmodified lines
1360 state->cursor_at_end_of_line = 0;1361 state->initialized = 1;1362 state->single_line = (unsigned char) is_single_line;1363 state->insert_mode = 0;1364 state->row_count_per_page = 0;1365}13661367// API initialize1368static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)
61 unmodified lines
modified:stb_tilemap_editor.hnot shown

Its text is not shown, modified.

modified:stb_truetype.hnot shown

Its text is not shown, modified.

modified:stb_vorbis.cnot shown

Its text is not shown, modified.