#include "v8/v8.h"
#include <string>
#include "windows.h"
#include "stdio.h"
#include "process.h"
#include <map>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")

using namespace std;
using namespace v8;

LONG n_active = 0;

typedef map<string, string> s2s;
typedef s2s::iterator is2s;

Handle<Value> cppfoo(const Arguments& args)
{
        Handle<Context> context = Context::GetCurrent();
        {
                v8::Unlocker unlocker;
                Sleep(0);
        }
        return Integer::New(1);
 }



void thread (void* p)
{
        int m = (int) p;

        InterlockedIncrement(&n_active);

        Persistent<Context> context;
        Persistent<Function> foo;

        {
                v8::Locker locker;
                HandleScope handle_scope;

                Handle<ObjectTemplate> global = ObjectTemplate::New();
                global->Set(String::NewSymbol("cppfoo"), FunctionTemplate::New
(cppfoo));

                context = Context::New(NULL, global);
                TryCatch try_catch;
                Context::Scope context_scope(context);

                Local<Script> script = Script::Compile(String::New("function 
foo(o)
{ var a = new Array(10000); a = []; "
                        "var s = ''; for (var i in o) s += o[i]; return s + 
cppfoo(); }"));
                Local<Value> result = script->Run();
                foo = Persistent<Function>::New(Handle<Function>::Cast(context-
>Global()->Get(String::New("foo"))));
        }

        s2s meta;
        const char* names[] = {"A","B","C","D","E","F","G","H"};
        for (int i = 0; i < 8; i++)
                meta[ names[i] ] = "gone fishing";

        while (m--)
        {



                int n_calls = 1000;
                while (n_calls--)
                {

                        {
                                v8::Locker locker;
                                HandleScope handle_scope;
                                Context::Scope context_scope(context);
                                TryCatch try_catch;

                                Local<Object> obj = Object::New();

                                for (is2s I = meta.begin(); I != meta.end(); 
I++)
                                        obj->Set (
                                                
String::NewSymbol(I->first.data(), I->first.size()),
                                                String::New(I->second.data(), 
I->second.size())
                                        );

                                const int argc = 1;
                                Local<Value> argv[argc] = { obj };
                                Local<Value> result = 
foo->Call(context->Global(), argc, argv);

                        }

                        Sleep(0);

                }

                Sleep(1);

        }

        v8::Locker locker;
        foo.Dispose();
        context.Dispose();

        InterlockedDecrement(&n_active);

}


int usage()
{
        printf("Usage:\r\n\r\n        memeater.exe <num_of_threads>\r\n\r
\n");
        return 1;
}

int main(int argc, char *argv[])
{
        if (argc <= 1) return usage();

        int n = atoi(*++argv);

        printf("running %d threads\n", n);

        while (true)
        {
                if (n_active < n)
                {
                        _beginthread(thread, 0, (void*) 100);
                        printf("another thread started\n");
                }

                Sleep(1000);
        }

        return 0;

}

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to