|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.modules.bz2; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.BZ2Compressor; |
| 44 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError; |
| 45 | +import static com.oracle.graal.python.builtins.modules.bz2.Bz2Nodes.BZ_OK; |
| 46 | +import static com.oracle.graal.python.builtins.modules.bz2.Bz2Nodes.errorHandling; |
| 47 | +import static com.oracle.graal.python.nodes.ErrorMessages.COMPRESSLEVEL_MUST_BE_BETWEEN_1_AND_9; |
| 48 | +import static com.oracle.graal.python.nodes.ErrorMessages.COMPRESSOR_HAS_BEEN_FLUSHED; |
| 49 | +import static com.oracle.graal.python.nodes.ErrorMessages.REPEATED_CALL_TO_FLUSH; |
| 50 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__; |
| 51 | + |
| 52 | +import java.util.List; |
| 53 | + |
| 54 | +import com.oracle.graal.python.PythonLanguage; |
| 55 | +import com.oracle.graal.python.annotations.ArgumentClinic; |
| 56 | +import com.oracle.graal.python.builtins.Builtin; |
| 57 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 58 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 59 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 60 | +import com.oracle.graal.python.builtins.objects.bytes.BytesNodes; |
| 61 | +import com.oracle.graal.python.builtins.objects.bytes.PBytes; |
| 62 | +import com.oracle.graal.python.builtins.objects.bytes.PBytesLike; |
| 63 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; |
| 64 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 65 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 66 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode; |
| 67 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 68 | +import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; |
| 69 | +import com.oracle.graal.python.runtime.NFIBz2Support; |
| 70 | +import com.oracle.graal.python.runtime.NativeLibrary; |
| 71 | +import com.oracle.graal.python.runtime.PythonContext; |
| 72 | +import com.oracle.truffle.api.dsl.Cached; |
| 73 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
| 74 | +import com.oracle.truffle.api.dsl.CachedContext; |
| 75 | +import com.oracle.truffle.api.dsl.Fallback; |
| 76 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 77 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 78 | +import com.oracle.truffle.api.dsl.Specialization; |
| 79 | +import com.oracle.truffle.api.profiles.ConditionProfile; |
| 80 | + |
| 81 | +@CoreFunctions(extendClasses = BZ2Compressor) |
| 82 | +public class BZ2CompressorBuiltins extends PythonBuiltins { |
| 83 | + @Override |
| 84 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 85 | + return BZ2CompressorBuiltinsFactory.getFactories(); |
| 86 | + } |
| 87 | + |
| 88 | + @Builtin(name = __INIT__, minNumOfPositionalArgs = 1, parameterNames = {"$self", "compresslevel"}) |
| 89 | + @ArgumentClinic(name = "compresslevel", conversion = ArgumentClinic.ClinicConversion.Int, defaultValue = "9", useDefaultForNone = true) |
| 90 | + @GenerateNodeFactory |
| 91 | + public abstract static class InitNode extends PythonBinaryClinicBuiltinNode { |
| 92 | + |
| 93 | + @Override |
| 94 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 95 | + return BZ2CompressorBuiltinsClinicProviders.InitNodeClinicProviderGen.INSTANCE; |
| 96 | + } |
| 97 | + |
| 98 | + @Specialization(guards = {"compresslevel >= 1", "compresslevel <= 9"}) |
| 99 | + PNone init(BZ2Object.BZ2Compressor self, int compresslevel, |
| 100 | + @CachedContext(PythonLanguage.class) PythonContext ctxt, |
| 101 | + @Cached NativeLibrary.InvokeNativeFunction createStream, |
| 102 | + @Cached NativeLibrary.InvokeNativeFunction compressInit, |
| 103 | + @Cached ConditionProfile errProfile) { |
| 104 | + NFIBz2Support bz2Support = ctxt.getNFIBz2Support(); |
| 105 | + Object bzst = bz2Support.createStream(createStream); |
| 106 | + int err = bz2Support.compressInit(bzst, compresslevel, compressInit); |
| 107 | + if (errProfile.profile(err != BZ_OK)) { |
| 108 | + errorHandling(err, getRaiseNode()); |
| 109 | + } |
| 110 | + self.init(bzst, bz2Support); |
| 111 | + return PNone.NONE; |
| 112 | + } |
| 113 | + |
| 114 | + @SuppressWarnings("unused") |
| 115 | + @Fallback |
| 116 | + Object err(Object self, Object compresslevel) { |
| 117 | + throw raise(ValueError, COMPRESSLEVEL_MUST_BE_BETWEEN_1_AND_9); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Builtin(name = "compress", minNumOfPositionalArgs = 2, parameterNames = {"$self", "$data"}) |
| 122 | + @GenerateNodeFactory |
| 123 | + abstract static class CompressNode extends PythonBinaryBuiltinNode { |
| 124 | + |
| 125 | + @Specialization(guards = {"!self.isFlushed()"}) |
| 126 | + PBytes doNativeBytes(BZ2Object.BZ2Compressor self, PBytesLike data, |
| 127 | + @Shared("c") @CachedContext(PythonLanguage.class) PythonContext ctxt, |
| 128 | + @Cached SequenceStorageNodes.GetInternalByteArrayNode toBytes, |
| 129 | + @Cached SequenceStorageNodes.LenNode lenNode, |
| 130 | + @Shared("c") @Cached Bz2Nodes.Bz2NativeCompress compress) { |
| 131 | + synchronized (self) { |
| 132 | + byte[] bytes = toBytes.execute(data.getSequenceStorage()); |
| 133 | + int len = lenNode.execute(data.getSequenceStorage()); |
| 134 | + return factory().createBytes(compress.compress(self, ctxt, bytes, len)); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Specialization(guards = {"!self.isFlushed()"}) |
| 139 | + PBytes doNativeObject(BZ2Object.BZ2Compressor self, Object data, |
| 140 | + @Shared("c") @CachedContext(PythonLanguage.class) PythonContext ctxt, |
| 141 | + @Cached BytesNodes.ToBytesNode toBytes, |
| 142 | + @Shared("c") @Cached Bz2Nodes.Bz2NativeCompress compress) { |
| 143 | + synchronized (self) { |
| 144 | + byte[] bytes = toBytes.execute(data); |
| 145 | + int len = bytes.length; |
| 146 | + return factory().createBytes(compress.compress(self, ctxt, bytes, len)); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @SuppressWarnings("unused") |
| 151 | + @Specialization(guards = "self.isFlushed()") |
| 152 | + PNone error(BZ2Object.BZ2Compressor self, Object data) { |
| 153 | + throw raise(ValueError, COMPRESSOR_HAS_BEEN_FLUSHED); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Builtin(name = "flush", minNumOfPositionalArgs = 1, parameterNames = {"$self"}) |
| 158 | + @GenerateNodeFactory |
| 159 | + abstract static class FlushNode extends PythonUnaryBuiltinNode { |
| 160 | + |
| 161 | + @Specialization(guards = {"!self.isFlushed()"}) |
| 162 | + PBytes doit(BZ2Object.BZ2Compressor self, |
| 163 | + @CachedContext(PythonLanguage.class) PythonContext ctxt, |
| 164 | + @Cached Bz2Nodes.Bz2NativeCompress compress) { |
| 165 | + synchronized (self) { |
| 166 | + self.setFlushed(); |
| 167 | + return factory().createBytes(compress.flush(self, ctxt)); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @SuppressWarnings("unused") |
| 172 | + @Specialization(guards = "self.isFlushed()") |
| 173 | + PNone error(BZ2Object.BZ2Compressor self) { |
| 174 | + throw raise(ValueError, REPEATED_CALL_TO_FLUSH); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments