I tried what you described and here is the code.
Configuration:
var li = Document.LayeredImage;
var n = li.Layers;
var names = [];
for (var i = 0; i < n; ++i)
names.push(li.GetLayerName(i));
Configuration.Add1ofNPicker("l1", "Layer 1", "", names, 0, true);
Configuration.Add1ofNPicker("l2", "Layer 2", "", names, n > 1 ? 1 : 0, true);
Execution:
var li = Document.LayeredImage;
var nX = li.sizeX;
var nY = li.sizeY;
var n = li.Layers;
var l1 = li.GetLayer(Configuration.l1);
var l2 = li.GetLayer(Configuration.l2);
// create new layer
var wiz = Application.CreateWizard("35AFBDB5-1B26-4195-8786-83C8E1CBC08E");
wiz.SizeX = nX;
wiz.SizeY = nY;
li.CreateLayer(-1, wiz, "35AFBDB5-1B26-4195-8786-83C8E1CBC08E");
var newlayer = li.GetLayer(0);
// copy l2 to new layer
Blender.Compose(newlayer, 0, 0, nX, nY, l2, 0, 0, 0, Blender.OpSrc);
// copy alpha channer from l1 to the new layer
Blender.Compose(newlayer, 0, 0, nX, nY, l1, 0, 0, 0, Blender.OpMapChannels,
Blender.MapChannels(Blender.ChEmpty, Blender.ChEmpty, Blender.ChEmpty, Blender.ChA,
Blender.ChEmpty, Blender.ChEmpty, Blender.ChEmpty, Blender.ChA));
I occasionally encounter the error you describe and I do not know why. It seems that the Microsoft JavaScript engine is sometimes confused when dealing with the global objects exposed by RW. It usually happens with the Context object and it helps to just mention the global object in the script before actually using it by just typing
Context;
in the beginning of the script.
Maybe the problem was with removing the "Extract document part" item?