text: "use strict"
  
  var compile = require("./compile.js")
  
  function createThunk(proc) {
    var code = ["'use strict'", "var CACHED={}"]
    var vars = []
    var thunkName = proc.funcName + "_cwise_thunk"
    
    //Build thunk
    code.push(["return function ", thunkName, "(", proc.shimArgs.join(","), "){"].join(""))
    var typesig = []
    var string_typesig = []
    var proc_args = [["array",proc.arrayArgs[0],".shape"].join("")]
    for(var i=0; i<proc.arrayArgs.length; ++i) {
      var j = proc.arrayArgs[i]
      vars.push(["t", j, "=array", j, ".dtype,",
                 "r", j, "=array", j, ".order"].join(""))
      typesig.push("t" + j)
      typesig.push("r" + j)
      string_typesig.push("t"+j)
      string_typesig.push("r"+j+".join()")
      proc_args.push("array" + j + ".data")
      proc_args.push("array" + j + ".stride")
      proc_args.push("array" + j + ".offset|0")
    }
    for(var i=0; i<proc.scalarArgs.length; ++i) {
      proc_args.push("scalar" + proc.scalarArgs[i])
    }
    vars.push(["type=[", string_typesig.join(","), "].join()"].join(""))
    vars.push("proc=CACHED[type]")
    code.push("var " + vars.join(","))
    
    code.push(["if(!proc){",
               "CACHED[type]=proc=compile([", typesig.join(","), "])}",
               "return proc(", proc_args.join(","), ")}"].join(""))
  
    if(proc.debug) {
      console.log("Generated thunk:", code.join("\n"))
    }
    
    //Compile thunk
    var thunk = new Function("compile", code.join("\n"))
    return thunk(compile.bind(undefined, proc))
  }
  
  module.exports = createThunk
  