[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

8chan Bitcoin address: 1NpQaXqmCBji6gfX8UgaQEmEstvVY7U32C
The next generation of Infinity is here (discussion) (contribute)
A message from @CodeMonkeyZ, 2ch lead developer: "How Hiroyuki Nishimura will sell 4chan data"
Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


File: 1430591591042.jpg (328.27 KB, 900x1980, 5:11, 1374418964734.jpg)

cec850 No.2368

Share something you learnt recently that you thought was cool and explain it.

Doesn't matter what it is. Just state what it does, why you think it's cool, and explain it! Explaining things to others is how you master your own learnings and also has a neat by-product of helping others.

[Web development]

If you use a templating langauge (eg. Handlebars, Mustache, Jade templates) or anything else that injects html elements into the page after it's already loaded, regular javascript selectors (eg. $('element').click) will not work.

They don't work cause the element wasn't mapped when the DOM first loaded. Using this you can select them anyway though:

<code>

$(document).on('event', 'element', 'yourFunction'); </code>

example:

<code>

$(document).on('click', '.aDiv', 'yourFunction');

yourFunction = function () {

$(this).css('color','red');

};

</code>

So basically, when you do something, and that something happens have the class or id of the element you're looking for, run a function.

pic unreleated

1d0e40 No.2372

>>2368

>Share something you learnt recently that you thought was cool and explain it.

I recently learned about the magic and wonders of "trampolines".

Except I used goto instead of longjmp(). Sue me.

In my particular case:

int oversimplified_example(some, arguments, and, shit)
{
int err,
mode;

/* Variables to store state information, plus some initialization code here… */

READ_AND_DISPATCH: /* ← Reads some stuff from the input file, then restores the previous state */
err = read(…);
if( err ) goto END;

switch( mode ) {
case 1: goto SUB_A;
/* … */
default: goto ERROR;
}
SUB_A:
/* Do some operation */
mode = 3; /* "Call" a different "function" */
goto READ_AND_DISPATCH;
ERROR:
/* This handles a special kind of error the caller doesn't need to know about */
err = 0;
END:
return err;
}

What makes this neat is that it can be used to write a system of mutually recursive "functions", but without worrying about overflowing the stack.

Oh, plus, since they all share the same scope, I don't have to write proper functions with massive argument lists that trail off the screen.


05833e No.2377

>>2372

I don't get it. I'm not sure thats a trampoline either


7481e7 No.2380

>switch( mode ) {

> …

> mode = 3; /* "Call" a different "function" */

kill me now.


1d0e40 No.2381

>>2377

>I don't get it

I don't have stdio.h for this particular project, so what ended up happening is a lot of functions like this:

static int _discardwhitespace(STREAM *source)
{
int err; /* Error(s)? */

CHECKSTREAM(source, IO_MODE_IN);

err = 0;

do {
ITERATE_STREAM(source) {
if( source->next[0] > 32 )
goto END;
}

err = flushstream(source);
} while( !err );

END:
return err;
}

This is fine for simple things, but when it came to parsing string literals, I needed something that didn't involve

copy-pasting all that boilerplate all over the place; by abusing goto I was able to have all of the rules share the same boilerplate.

>I'm not sure thats a trampoline either

>https://en.wikipedia.org/wiki/Tail_call#Through_trampolining

>All functions are entered via the trampoline. When a function has to call another, instead of calling it directly it returns the address of the function to be called, the arguments to be used, and so on, to the trampoline. This ensures that the C stack does not grow and iteration can continue indefinitely.

Which is basically what I've done here.

For example, when the "scan" rule encounters a backslash in the string literal, it copies anything it's already read to the buffer, then:

mode = PLMODE_ESCAPE;
goto READ_AND_DISPATCH;

The "dispatch" rule reads some more data from the stream, then locates the "escape" rule and jumps to it.

That rule then handles [whatever escape sequence], and passes the continuation back to the "scan" rule.




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]