Package org.perl6.nqp.sixmodel

Examples of org.perl6.nqp.sixmodel.KnowHOWMethods


import org.perl6.nqp.sixmodel.reprs.KnowHOWREPRInstance;

public class KnowHOWBootstrapper {
    public static void bootstrap(ThreadContext tc)
    {
        CompilationUnit knowhowUnit = new KnowHOWMethods();
        knowhowUnit.initializeCompilationUnit(tc);
        bootstrapKnowHOW(tc, knowhowUnit);
        bootstrapKnowHOWAttribute(tc, knowhowUnit);
       
        tc.gc.BOOTArray = bootType(tc, "BOOTArray", "VMArray");
        tc.gc.BOOTHash = bootType(tc, "BOOTHash", "VMHash");
View Full Code Here


            }
            else if (cmdStrings[1].equals("run")) {
                String[] argv = new String[cmdStrings.length - 3];
                System.arraycopy(cmdStrings, 2, argv, 0, argv.length);

                GlobalContext gc = new GlobalContext();
                gc.in = new ByteArrayInputStream(new byte[0]);
                gc.out = gc.err = new PrintStream( Channels.newOutputStream(sock), true, "UTF-8" );
                gc.interceptExit = true;
                gc.sharingHint = true;
View Full Code Here

        writeByteBuffer(tc, task, buffer);
    }

    private void writeByteBuffer(final ThreadContext tc, final AsyncTaskInstance task, ByteBuffer buffer) {
        try {
            HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
            final SixModelObject Array = hllConfig.listType;
            final SixModelObject Int = hllConfig.intBoxType;
            final SixModelObject Null = hllConfig.nullValue;
            final SixModelObject Str = hllConfig.strBoxType;
View Full Code Here

            throw ExceptionHandling.dieInternal(tc, e);
        }
    }

    public void readChars(final ThreadContext tc, final AsyncTaskInstance task) {
        HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
        final SixModelObject Str = hllConfig.strBoxType;

        readSocket(tc, task, new Decoder () {
            final CharBuffer decodedBuffer = CharBuffer.allocate(32768);
View Full Code Here

    }

    private void readSocket(final ThreadContext tc, final AsyncTaskInstance task, final Decoder decoder) {
        final ByteBuffer readBuffer = ByteBuffer.allocate(32768);

        HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
        final SixModelObject Array = hllConfig.listType;
        final SixModelObject Int = hllConfig.intBoxType;
        final SixModelObject Str = hllConfig.strBoxType;
        final SixModelObject Null = hllConfig.nullValue;
View Full Code Here

            /* Create a context and set it up. */
            CallFrame ctx = new CallFrame();
            ctx.tc = tc;
            ctx.codeRef = staticCode;
            StaticCodeInfo sci = staticCode.staticInfo;
            if (sci.oLexicalNames != null)
                ctx.oLex = sci.oLexStatic.clone();
            if (sci.iLexicalNames != null)
                ctx.iLex = new long[sci.iLexicalNames.length];
            if (sci.nLexicalNames != null)
                ctx.nLex = new double[sci.nLexicalNames.length];
            if (sci.sLexicalNames != null)
                ctx.sLex = new String[sci.sLexicalNames.length];

            /* Set context data read position, and set current read buffer to the correct thing. */
            orig.position(contextDataOffset + orig.getInt());

            /* Deserialize lexicals. */
            long syms = orig.getLong();
            for (long j = 0; j < syms; j++) {
                String sym = readStr();
                Integer idx;
                if ((idx = sci.oTryGetLexicalIdx(sym)) != null)
                    ctx.oLex[idx] = readRef();
                else if ((idx = sci.iTryGetLexicalIdx(sym)) != null)
                    ctx.iLex[idx] = orig.getLong();
                else if ((idx = sci.nTryGetLexicalIdx(sym)) != null)
                    ctx.nLex[idx] = orig.getDouble();
                else if ((idx = sci.sTryGetLexicalIdx(sym)) != null)
                    ctx.sLex[idx] = readStr();
                else
                    throw new RuntimeException("Failed to deserialize lexical " + sym);
            }

View Full Code Here

            final SixModelObject Str = hllConfig.strBoxType;

            @Override
            public void completed(AsynchronousSocketChannel channel, AsyncTaskInstance task) {
                listenChan.accept(task, this);
                ThreadContext curTC = tc.gc.getCurrentThreadContext();

                AsyncSocketHandle handle = new AsyncSocketHandle(curTC, channel);
                IOHandleInstance ioHandle = (IOHandleInstance) IOType.st.REPR.allocate(curTC,
                        IOType.st);
                ioHandle.handle = handle;

                SixModelObject result = Array.st.REPR.allocate(curTC, Array.st);
                result.push_boxed(curTC, task.schedulee);
                result.push_boxed(curTC, ioHandle);
                result.push_boxed(curTC, Null);

                ((ConcBlockingQueueInstance) task.queue).push_boxed(curTC, result);
            }

            @Override
            public void failed(Throwable exc, AsyncTaskInstance task) {
                ThreadContext curTC = tc.gc.getCurrentThreadContext();
                SixModelObject result = Array.st.REPR.allocate(curTC, Array.st);
                result.push_boxed(curTC, task.schedulee);
                result.push_boxed(curTC, IOType);
                result.push_boxed(curTC, Ops.box_s(exc.getMessage(), Str, curTC));
            }
View Full Code Here

            final CompletionHandler<Integer, SlurpState> ch = new CompletionHandler<Integer, SlurpState>() {
                public void completed(Integer bytes, SlurpState ss) {
                    if (ss.bb.position() == ss.expected) {
                        try {
                            /* We're done. Decode and box. */
                            ThreadContext curTC = tc.gc.getCurrentThreadContext();
                            ss.bb.flip();
                            String decoded = dec.decode(ss.bb).toString();
                            SixModelObject boxed = Ops.box_s(decoded, Str, curTC);
                           
                            /* Call done handler. */
                            Ops.invokeDirect(curTC, done, slurpResultCSD, new Object[] { boxed });
                        } catch (IOException e) {
                            failed(e, ss);
                        }
                    }
                    else {
                        /* Need to read some more. */
                        chan.read(ss.bb, ss.bb.position(), ss, this);
                    }
                }
               
                public void failed(Throwable exc, SlurpState ss) {
                    /* Box error. */
                    ThreadContext curTC = tc.gc.getCurrentThreadContext();
                    SixModelObject boxed = Ops.box_s(exc.toString(), Str, curTC);
                   
                    /* Call error handler. */
                    Ops.invokeDirect(curTC, error, slurpResultCSD, new Object[] { boxed });
                }
View Full Code Here

            final CompletionHandler<Integer, SpurtState> ch = new CompletionHandler<Integer, SpurtState>() {
                public void completed(Integer bytes, SpurtState ss) {
                    if (ss.bb.position() == ss.expected) {
                        /* Done. Call done handler. */
                        ThreadContext curTC = tc.gc.getCurrentThreadContext();
                        Ops.invokeDirect(curTC, done, spurtResultCSD, new Object[] { });
                    }
                    else {
                        /* Need to write some more. */
                        chan.write(ss.bb, ss.bb.position(), ss, this);
                    }
                }

                public void failed(Throwable exc, SpurtState ss) {
                    /* Box error. */
                    ThreadContext curTC = tc.gc.getCurrentThreadContext();
                    SixModelObject boxed = Ops.box_s(exc.toString(), Str, curTC);

                    /* Call error handler. */
                    Ops.invokeDirect(curTC, error, spurtErrorCSD, new Object[] { boxed });
                }
View Full Code Here

        ls.position = 0;
       
        final CompletionHandler<Integer, LinesState> ch = new CompletionHandler<Integer, LinesState>() {
            public void completed(Integer bytes, LinesState ss) {
                try {
                    ThreadContext curTC = tc.gc.getCurrentThreadContext();
                   
                    /* If we're read all, send done notification. */
                    if (bytes == -1) {
                        /* we may have a bit of non-linebreak-terminated data to spit out */
                        if (ss.lineChunks.size() > 0) {
                            /* decode, box and enqueue. no need to chomp. */
                            String decoded = ss.lineChunks.size() == 1
                                ? dec.decode(ss.lineChunks.get(0)).toString()
                                : decodeBuffers(ss.lineChunks, ss.total);
                            /* Box and enqueue. */
                            queue.put(Ops.box_s(decoded, Str, curTC));
                        }
                        Ops.invokeDirect(curTC, done, linesDoneCSD, new Object[] { });
                        return;
                    }
                   
                    /* Flip the just-read buffer. */
                    ss.readBuffer.flip();
                   
                    /* Look for lines. */
                    while (true) {
                        /* Hunt a line boundary. */
                        boolean foundLine = false;
                        int start = ss.readBuffer.position();
                        int end = start;
                        while (!foundLine && end < ss.readBuffer.limit()) {
                            if (ss.readBuffer.get(end) == '\n')
                                foundLine = true;
                            end++;
                        }
                       
                        /* Copy what we found into the results. */
                        byte[] lineBytes = new byte[end - start];
                        ss.readBuffer.get(lineBytes);
                        ss.lineChunks.add(ByteBuffer.wrap(lineBytes));
                        ss.total += lineBytes.length;
                       
                        /* If we found a line... */
                        if (foundLine) {
                            /* Decode. */
                            String decoded = ss.lineChunks.size() == 1
                                ? dec.decode(ss.lineChunks.get(0)).toString()
                                : decodeBuffers(ss.lineChunks, ss.total);
                           
                            /* Chomp if needed. */
                            if (chomp) {
                                int decLen = decoded.length();
                                int cutChars = 0;
                                if (decLen >= 1 && decoded.charAt(decLen - 1) == '\n') {
                                    cutChars++;
                                    if (decLen >= 2 && decoded.charAt(decLen - 2) == '\r')
                                        cutChars++;
                                }
                                if (cutChars > 0)
                                    decoded = decoded.substring(0, decLen - cutChars);
                            }
                           
                            /* Box and enqueue. */
                            queue.put(Ops.box_s(decoded, Str, curTC));
                           
                            /* Reset for next line. */
                            ss.lineChunks.clear();
                            ss.total = 0;
                        }
                        else {
                            /* Couldn't find an end of line, stop looping. */
                            break;
                        }
                    }
                   
                    /* Read more. */
                    ls.position += bytes;
                    ls.readBuffer = ByteBuffer.allocate(32768);
                    chan.read(ls.readBuffer, ls.position, ls, this);
                } catch (IOException e) {
                    failed(e, ls);
                } catch (InterruptedException e) {
                    failed(e, ls);
                }
            }
   
            private String decodeBuffers(ArrayList<ByteBuffer> buffers, int total) throws IOException {
                // Copy to a single buffer and decode (could be smarter, but need
                // to be wary as UTF-8 chars may span a buffer boundary).
                ByteBuffer allBytes = ByteBuffer.allocate(total);
                for (ByteBuffer bb : buffers)
                    allBytes.put(bb.array(), 0, bb.limit());
                allBytes.rewind();
                return dec.decode(allBytes).toString();
            }
           
            public void failed(Throwable exc, LinesState ss) {
                /* Box error. */
                ThreadContext curTC = tc.gc.getCurrentThreadContext();
                SixModelObject boxed = Ops.box_s(exc.toString(), Str, curTC);
               
                /* Call error handler. */
                Ops.invokeDirect(curTC, error, linesErrorCSD, new Object[] { boxed });
            }
View Full Code Here

TOP

Related Classes of org.perl6.nqp.sixmodel.KnowHOWMethods

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.